# Generated by Django 5.2.8 on 2025-12-11 19:56

from django.db import migrations, models


def add_column_if_not_exists(table, column, column_type, default):
    """Generate SQL to conditionally add a column."""
    return f"""
        DO $$
        BEGIN
            IF NOT EXISTS (
                SELECT 1 FROM information_schema.columns
                WHERE table_schema = current_schema()
                  AND table_name = '{table}'
                  AND column_name = '{column}'
            ) THEN
                ALTER TABLE {table} ADD COLUMN {column} {column_type} DEFAULT {default} NOT NULL;
            END IF;
        END $$;
    """


class Migration(migrations.Migration):

    dependencies = [
        ('schedule', '0035_add_service_resource_fields'),
    ]

    operations = [
        # These columns may already exist in some schemas
        migrations.SeparateDatabaseAndState(
            state_operations=[
                migrations.AddField(
                    model_name='service',
                    name='prep_time',
                    field=models.PositiveIntegerField(default=0, help_text='Preparation time in minutes before the service'),
                ),
                migrations.AddField(
                    model_name='service',
                    name='takedown_time',
                    field=models.PositiveIntegerField(default=0, help_text='Cleanup/takedown time in minutes after the service'),
                ),
                migrations.AddField(
                    model_name='service',
                    name='reminder_enabled',
                    field=models.BooleanField(default=False, help_text='Whether to send reminder notifications for this service'),
                ),
                migrations.AddField(
                    model_name='service',
                    name='reminder_hours_before',
                    field=models.PositiveIntegerField(default=24, help_text='Hours before appointment to send reminder'),
                ),
                migrations.AddField(
                    model_name='service',
                    name='reminder_email',
                    field=models.BooleanField(default=True, help_text='Send reminder via email'),
                ),
                migrations.AddField(
                    model_name='service',
                    name='reminder_sms',
                    field=models.BooleanField(default=False, help_text='Send reminder via SMS'),
                ),
                migrations.AddField(
                    model_name='service',
                    name='thank_you_email_enabled',
                    field=models.BooleanField(default=False, help_text='Send thank you email after service completion'),
                ),
            ],
            database_operations=[
                migrations.RunSQL(
                    sql=add_column_if_not_exists('schedule_service', 'prep_time', 'INTEGER', '0'),
                    reverse_sql='ALTER TABLE schedule_service DROP COLUMN IF EXISTS prep_time;',
                ),
                migrations.RunSQL(
                    sql=add_column_if_not_exists('schedule_service', 'takedown_time', 'INTEGER', '0'),
                    reverse_sql='ALTER TABLE schedule_service DROP COLUMN IF EXISTS takedown_time;',
                ),
                migrations.RunSQL(
                    sql=add_column_if_not_exists('schedule_service', 'reminder_enabled', 'BOOLEAN', 'FALSE'),
                    reverse_sql='ALTER TABLE schedule_service DROP COLUMN IF EXISTS reminder_enabled;',
                ),
                migrations.RunSQL(
                    sql=add_column_if_not_exists('schedule_service', 'reminder_hours_before', 'INTEGER', '24'),
                    reverse_sql='ALTER TABLE schedule_service DROP COLUMN IF EXISTS reminder_hours_before;',
                ),
                migrations.RunSQL(
                    sql=add_column_if_not_exists('schedule_service', 'reminder_email', 'BOOLEAN', 'TRUE'),
                    reverse_sql='ALTER TABLE schedule_service DROP COLUMN IF EXISTS reminder_email;',
                ),
                migrations.RunSQL(
                    sql=add_column_if_not_exists('schedule_service', 'reminder_sms', 'BOOLEAN', 'FALSE'),
                    reverse_sql='ALTER TABLE schedule_service DROP COLUMN IF EXISTS reminder_sms;',
                ),
                migrations.RunSQL(
                    sql=add_column_if_not_exists('schedule_service', 'thank_you_email_enabled', 'BOOLEAN', 'FALSE'),
                    reverse_sql='ALTER TABLE schedule_service DROP COLUMN IF EXISTS thank_you_email_enabled;',
                ),
            ],
        ),
    ]
