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

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('schedule', '0034_add_purpose_to_timeblock'),
    ]

    operations = [
        # all_resources column already exists in some schemas, so we use
        # SeparateDatabaseAndState to sync Django's state without trying to
        # recreate the column. If the column doesn't exist, we add it.
        migrations.SeparateDatabaseAndState(
            state_operations=[
                migrations.AddField(
                    model_name='service',
                    name='all_resources',
                    field=models.BooleanField(default=True, help_text='If true, service can be performed by any resource. If false, use resource_ids field.'),
                ),
            ],
            database_operations=[
                # Use raw SQL to conditionally add the column only if it doesn't exist
                # IMPORTANT: Must check current_schema() for multi-tenant compatibility
                migrations.RunSQL(
                    sql="""
                        DO $$
                        BEGIN
                            IF NOT EXISTS (
                                SELECT 1 FROM information_schema.columns
                                WHERE table_schema = current_schema()
                                  AND table_name = 'schedule_service'
                                  AND column_name = 'all_resources'
                            ) THEN
                                ALTER TABLE schedule_service ADD COLUMN all_resources BOOLEAN DEFAULT TRUE NOT NULL;
                            END IF;
                        END $$;
                    """,
                    reverse_sql="""
                        ALTER TABLE schedule_service DROP COLUMN IF EXISTS all_resources;
                    """,
                ),
            ],
        ),
        migrations.SeparateDatabaseAndState(
            state_operations=[
                migrations.AddField(
                    model_name='service',
                    name='resource_ids',
                    field=models.JSONField(blank=True, default=list, help_text='List of specific resource IDs that can perform this service (only used if all_resources=False)'),
                ),
            ],
            database_operations=[
                # Use raw SQL to conditionally add the column only if it doesn't exist
                # IMPORTANT: Must check current_schema() for multi-tenant compatibility
                migrations.RunSQL(
                    sql="""
                        DO $$
                        BEGIN
                            IF NOT EXISTS (
                                SELECT 1 FROM information_schema.columns
                                WHERE table_schema = current_schema()
                                  AND table_name = 'schedule_service'
                                  AND column_name = 'resource_ids'
                            ) THEN
                                ALTER TABLE schedule_service ADD COLUMN resource_ids JSONB DEFAULT '[]'::jsonb NOT NULL;
                            END IF;
                        END $$;
                    """,
                    reverse_sql="""
                        ALTER TABLE schedule_service DROP COLUMN IF EXISTS resource_ids;
                    """,
                ),
            ],
        ),
    ]
