-- Fix ai_plans/user_ai_subscriptions/ai_feature_costs/ai_usage_logs timestamp
-- columns: they were created (by whatever earlier, pre-20260703210000
-- mechanism made these tables) as `TIMESTAMP` (no timezone), but
-- crates/db/src/models/ai_credits.rs maps every one of these to
-- `DateTime<Utc>`, which sqlx requires the column to be `TIMESTAMPTZ` for.
-- Discovered via crates/db/tests/ai_credits.rs failing with:
--   ColumnDecode { ... "TIMESTAMPTZ" is not compatible with SQL type "TIMESTAMP" }
-- This is a real, live bug, not just a test-setup gap: any production code
-- path that SELECTs current_period_start/current_period_end (or any of the
-- other columns below) via these structs would hit the same decode error.
--
-- `AT TIME ZONE 'UTC'` reinterprets the existing naive values as UTC wall-clock
-- time when adding the zone (the correct, lossless conversion given every
-- write path uses NOW()/DEFAULT NOW() with no other timezone handling
-- anywhere in this codebase) rather than converting through the session's
-- local timezone.

BEGIN;

ALTER TABLE ai_plans
    ALTER COLUMN created_at TYPE TIMESTAMPTZ USING created_at AT TIME ZONE 'UTC',
    ALTER COLUMN updated_at TYPE TIMESTAMPTZ USING updated_at AT TIME ZONE 'UTC';

ALTER TABLE user_ai_subscriptions
    ALTER COLUMN current_period_start TYPE TIMESTAMPTZ USING current_period_start AT TIME ZONE 'UTC',
    ALTER COLUMN current_period_end   TYPE TIMESTAMPTZ USING current_period_end   AT TIME ZONE 'UTC',
    ALTER COLUMN created_at           TYPE TIMESTAMPTZ USING created_at           AT TIME ZONE 'UTC',
    ALTER COLUMN updated_at           TYPE TIMESTAMPTZ USING updated_at           AT TIME ZONE 'UTC';

ALTER TABLE ai_feature_costs
    ALTER COLUMN created_at TYPE TIMESTAMPTZ USING created_at AT TIME ZONE 'UTC',
    ALTER COLUMN updated_at TYPE TIMESTAMPTZ USING updated_at AT TIME ZONE 'UTC';

ALTER TABLE ai_usage_logs
    ALTER COLUMN created_at TYPE TIMESTAMPTZ USING created_at AT TIME ZONE 'UTC';

COMMIT;
