-- ============================================
-- Migration: V20260120_002 - DOWN (Rollback)
-- Description: Revert billing.subscriptions structure
-- Changes (reversed):
--   - Remove stripe_subscription_id, stripe_customer_id columns
--   - Remove interval column
--   - Remove trial_start, cancel_at, cancel_reason columns
--   - Remove price_amount, currency columns
--   - Rename canceled_at -> cancelled_at
--   - Revert billing.subscription_status enum: 'trialing' -> 'trial'
-- ============================================

-- DOWN Migration (Rollback)
BEGIN;

-- ============================================
-- 1. Rename canceled_at -> cancelled_at (revert to British)
-- ============================================

DO $$
BEGIN
    IF EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_schema = 'billing' AND table_name = 'subscriptions' AND column_name = 'canceled_at'
    ) THEN
        ALTER TABLE billing.subscriptions RENAME COLUMN canceled_at TO cancelled_at;
        RAISE NOTICE 'Renamed canceled_at back to cancelled_at';
    END IF;
END $$;

-- ============================================
-- 2. Remove pricing columns
-- ============================================

ALTER TABLE billing.subscriptions DROP COLUMN IF EXISTS currency;
ALTER TABLE billing.subscriptions DROP COLUMN IF EXISTS price_amount;

-- ============================================
-- 3. Remove trial and cancellation columns
-- ============================================

ALTER TABLE billing.subscriptions DROP COLUMN IF EXISTS cancel_reason;
ALTER TABLE billing.subscriptions DROP COLUMN IF EXISTS cancel_at;
ALTER TABLE billing.subscriptions DROP COLUMN IF EXISTS trial_start;

-- ============================================
-- 4. Remove interval column
-- ============================================

ALTER TABLE billing.subscriptions DROP COLUMN IF EXISTS "interval";

-- ============================================
-- 5. Remove Stripe columns
-- ============================================

DROP INDEX IF EXISTS billing.idx_subscriptions_stripe;
ALTER TABLE billing.subscriptions DROP COLUMN IF EXISTS stripe_customer_id;
ALTER TABLE billing.subscriptions DROP COLUMN IF EXISTS stripe_subscription_id;

-- ============================================
-- 6. Revert subscription_status enum (trialing -> trial)
-- ============================================

DO $$
DECLARE
    has_trialing_value BOOLEAN;
BEGIN
    -- Check if 'trialing' exists in the enum (needs rollback)
    SELECT EXISTS (
        SELECT 1
        FROM pg_enum e
        JOIN pg_type t ON e.enumtypid = t.oid
        JOIN pg_namespace n ON t.typnamespace = n.oid
        WHERE n.nspname = 'billing'
        AND t.typname = 'subscription_status'
        AND e.enumlabel = 'trialing'
    ) INTO has_trialing_value;

    IF has_trialing_value THEN
        -- Create old enum type with 'trial'
        CREATE TYPE billing.subscription_status_old AS ENUM ('trial', 'active', 'past_due', 'cancelled', 'expired');

        -- Convert column to text
        ALTER TABLE billing.subscriptions
            ALTER COLUMN status TYPE VARCHAR(50) USING status::VARCHAR(50);

        -- Revert 'trialing' to 'trial'
        UPDATE billing.subscriptions
        SET status = 'trial'
        WHERE status = 'trialing';

        -- Drop current type
        DROP TYPE IF EXISTS billing.subscription_status;

        -- Rename old type back
        ALTER TYPE billing.subscription_status_old RENAME TO subscription_status;

        -- Convert column back to enum
        ALTER TABLE billing.subscriptions
            ALTER COLUMN status TYPE billing.subscription_status
            USING status::billing.subscription_status;

        -- Restore default
        ALTER TABLE billing.subscriptions
            ALTER COLUMN status SET DEFAULT 'trial'::billing.subscription_status;

        RAISE NOTICE 'Reverted subscription_status enum: trialing -> trial';
    END IF;
END $$;

RAISE NOTICE 'Rollback of V20260120_002 completed';

COMMIT;
