-- +goose Up
ALTER TABLE tenants
  ADD COLUMN IF NOT EXISTS billing_provider text NOT NULL DEFAULT 'paddle';

ALTER TABLE billing_snapshots
  ADD COLUMN IF NOT EXISTS billing_provider text NOT NULL DEFAULT 'paddle';

ALTER TABLE subscription_events
  ADD COLUMN IF NOT EXISTS billing_provider text NOT NULL DEFAULT 'paddle';

DO $$
BEGIN
  IF EXISTS (
    SELECT 1
    FROM information_schema.columns
    WHERE table_name = 'tenants' AND column_name = 'stripe_customer_id'
  ) THEN
    ALTER TABLE tenants RENAME COLUMN stripe_customer_id TO billing_customer_id;
  END IF;
END $$;

DO $$
BEGIN
  IF EXISTS (
    SELECT 1
    FROM information_schema.columns
    WHERE table_name = 'tenants' AND column_name = 'stripe_subscription_id'
  ) THEN
    ALTER TABLE tenants RENAME COLUMN stripe_subscription_id TO billing_subscription_id;
  END IF;
END $$;

DO $$
BEGIN
  IF EXISTS (
    SELECT 1
    FROM information_schema.columns
    WHERE table_name = 'billing_snapshots' AND column_name = 'stripe_customer_id'
  ) THEN
    ALTER TABLE billing_snapshots RENAME COLUMN stripe_customer_id TO billing_customer_id;
  END IF;
END $$;

DO $$
BEGIN
  IF EXISTS (
    SELECT 1
    FROM information_schema.columns
    WHERE table_name = 'billing_snapshots' AND column_name = 'stripe_subscription_id'
  ) THEN
    ALTER TABLE billing_snapshots RENAME COLUMN stripe_subscription_id TO billing_subscription_id;
  END IF;
END $$;

DO $$
BEGIN
  IF EXISTS (
    SELECT 1
    FROM information_schema.columns
    WHERE table_name = 'subscription_events' AND column_name = 'stripe_event_id'
  ) THEN
    ALTER TABLE subscription_events RENAME COLUMN stripe_event_id TO billing_provider_event_id;
  END IF;
END $$;

ALTER TABLE subscription_events DROP CONSTRAINT IF EXISTS subscription_events_stripe_event_id_key;
ALTER TABLE subscription_events
  ADD CONSTRAINT subscription_events_provider_event_key UNIQUE (billing_provider, billing_provider_event_id);

-- +goose Down
ALTER TABLE subscription_events DROP CONSTRAINT IF EXISTS subscription_events_provider_event_key;

DO $$
BEGIN
  IF EXISTS (
    SELECT 1
    FROM information_schema.columns
    WHERE table_name = 'subscription_events' AND column_name = 'billing_provider_event_id'
  ) THEN
    ALTER TABLE subscription_events RENAME COLUMN billing_provider_event_id TO stripe_event_id;
  END IF;
END $$;

DO $$
BEGIN
  IF EXISTS (
    SELECT 1
    FROM information_schema.columns
    WHERE table_name = 'billing_snapshots' AND column_name = 'billing_subscription_id'
  ) THEN
    ALTER TABLE billing_snapshots RENAME COLUMN billing_subscription_id TO stripe_subscription_id;
  END IF;
END $$;

DO $$
BEGIN
  IF EXISTS (
    SELECT 1
    FROM information_schema.columns
    WHERE table_name = 'billing_snapshots' AND column_name = 'billing_customer_id'
  ) THEN
    ALTER TABLE billing_snapshots RENAME COLUMN billing_customer_id TO stripe_customer_id;
  END IF;
END $$;

DO $$
BEGIN
  IF EXISTS (
    SELECT 1
    FROM information_schema.columns
    WHERE table_name = 'tenants' AND column_name = 'billing_subscription_id'
  ) THEN
    ALTER TABLE tenants RENAME COLUMN billing_subscription_id TO stripe_subscription_id;
  END IF;
END $$;

DO $$
BEGIN
  IF EXISTS (
    SELECT 1
    FROM information_schema.columns
    WHERE table_name = 'tenants' AND column_name = 'billing_customer_id'
  ) THEN
    ALTER TABLE tenants RENAME COLUMN billing_customer_id TO stripe_customer_id;
  END IF;
END $$;

ALTER TABLE subscription_events DROP COLUMN IF EXISTS billing_provider;
ALTER TABLE billing_snapshots DROP COLUMN IF EXISTS billing_provider;
ALTER TABLE tenants DROP COLUMN IF EXISTS billing_provider;
ALTER TABLE subscription_events
  ADD CONSTRAINT subscription_events_stripe_event_id_key UNIQUE (stripe_event_id);
