# Generated manually to change price/deposit_amount columns from decimal to integer cents
# This migration:
# 1. Renames price -> price_cents and deposit_amount -> deposit_amount_cents
# 2. Converts values from dollars (decimal) to cents (integer)

from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('schedule', '0031_convert_orphaned_staff_resources'),
    ]

    operations = [
        # Step 1: Rename price to price_cents and convert to cents
        migrations.RunSQL(
            sql="""
                -- Rename and convert price column to cents
                ALTER TABLE schedule_service
                RENAME COLUMN price TO price_cents;

                -- Convert from dollars (decimal) to cents (integer)
                -- Multiply by 100 to convert dollars to cents
                ALTER TABLE schedule_service
                ALTER COLUMN price_cents TYPE integer
                USING (COALESCE(price_cents, 0) * 100)::integer;
            """,
            reverse_sql="""
                -- Convert back from cents to dollars
                ALTER TABLE schedule_service
                ALTER COLUMN price_cents TYPE numeric(10,2)
                USING (price_cents / 100.0)::numeric(10,2);

                -- Rename back to price
                ALTER TABLE schedule_service
                RENAME COLUMN price_cents TO price;
            """,
        ),
        # Step 2: Rename deposit_amount to deposit_amount_cents and convert to cents
        migrations.RunSQL(
            sql="""
                -- Rename deposit_amount column to deposit_amount_cents
                ALTER TABLE schedule_service
                RENAME COLUMN deposit_amount TO deposit_amount_cents;

                -- Convert from dollars (decimal) to cents (integer)
                ALTER TABLE schedule_service
                ALTER COLUMN deposit_amount_cents TYPE integer
                USING (COALESCE(deposit_amount_cents, 0) * 100)::integer;

                -- Allow NULL values (since original allowed NULL)
                ALTER TABLE schedule_service
                ALTER COLUMN deposit_amount_cents DROP NOT NULL;
            """,
            reverse_sql="""
                -- Convert back from cents to dollars
                ALTER TABLE schedule_service
                ALTER COLUMN deposit_amount_cents TYPE numeric(10,2)
                USING (deposit_amount_cents / 100.0)::numeric(10,2);

                -- Rename back to deposit_amount
                ALTER TABLE schedule_service
                RENAME COLUMN deposit_amount_cents TO deposit_amount;
            """,
        ),
    ]
