/**
 * Migration: Create Subscriptions Table
 *
 * This table stores organization subscription information including
 * plan, status, and Stripe integration data.
 */

import { MigrationInterface, QueryRunner } from 'typeorm';

export class CreateSubscriptions1738000000001 implements MigrationInterface {
  name = 'CreateSubscriptions1738000000001';

  public async up(queryRunner: QueryRunner): Promise<void> {
    // Create subscription_plan enum
    await queryRunner.query(`
      CREATE TYPE "subscription_plan_enum" AS ENUM ('FREE', 'STARTER', 'PRO', 'ENTERPRISE')
    `);

    // Create subscription_status enum
    await queryRunner.query(`
      CREATE TYPE "subscription_status_enum" AS ENUM (
        'ACTIVE',
        'PAST_DUE',
        'CANCELED',
        'INCOMPLETE',
        'INCOMPLETE_EXPIRED',
        'TRIALING',
        'UNPAID',
        'PAUSED'
      )
    `);

    // Create subscriptions table
    await queryRunner.query(`
      CREATE TABLE "subscriptions" (
        "id" UUID NOT NULL DEFAULT uuid_generate_v4(),
        "organization_id" UUID NOT NULL,

        -- Plan information
        "plan" subscription_plan_enum NOT NULL DEFAULT 'FREE',
        "status" subscription_status_enum NOT NULL DEFAULT 'ACTIVE',

        -- Stripe integration
        "stripe_customer_id" VARCHAR(255) NULL,
        "stripe_subscription_id" VARCHAR(255) NULL,

        -- Billing period
        "current_period_start" TIMESTAMP NULL,
        "current_period_end" TIMESTAMP NULL,
        "cancel_at_period_end" BOOLEAN NOT NULL DEFAULT FALSE,

        -- Timestamps
        "created_at" TIMESTAMP NOT NULL DEFAULT NOW(),
        "updated_at" TIMESTAMP NOT NULL DEFAULT NOW(),

        CONSTRAINT "pk_subscriptions" PRIMARY KEY ("id"),
        CONSTRAINT "uq_subscriptions_organization_id" UNIQUE ("organization_id"),
        CONSTRAINT "uq_subscriptions_stripe_subscription_id" UNIQUE ("stripe_subscription_id"),
        CONSTRAINT "fk_subscriptions_organization" FOREIGN KEY ("organization_id")
          REFERENCES "organizations"("id") ON DELETE CASCADE
      )
    `);

    // Create indexes
    await queryRunner.query(`
      CREATE INDEX "idx_subscriptions_organization_id" ON "subscriptions" ("organization_id")
    `);
    await queryRunner.query(`
      CREATE INDEX "idx_subscriptions_stripe_customer_id" ON "subscriptions" ("stripe_customer_id")
    `);
    await queryRunner.query(`
      CREATE INDEX "idx_subscriptions_stripe_subscription_id" ON "subscriptions" ("stripe_subscription_id")
    `);
    await queryRunner.query(`
      CREATE INDEX "idx_subscriptions_plan" ON "subscriptions" ("plan")
    `);
    await queryRunner.query(`
      CREATE INDEX "idx_subscriptions_status" ON "subscriptions" ("status")
    `);

    // Add comments
    await queryRunner.query(`
      COMMENT ON TABLE "subscriptions" IS 'Organization subscriptions for licensing system'
    `);
    await queryRunner.query(`
      COMMENT ON COLUMN "subscriptions"."plan" IS 'Subscription plan: FREE (2 users), STARTER (5), PRO (20), ENTERPRISE (unlimited)'
    `);
    await queryRunner.query(`
      COMMENT ON COLUMN "subscriptions"."cancel_at_period_end" IS 'If true, subscription will be canceled at the end of current period'
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`DROP TABLE IF EXISTS "subscriptions" CASCADE`);
    await queryRunner.query(`DROP TYPE IF EXISTS "subscription_status_enum"`);
    await queryRunner.query(`DROP TYPE IF EXISTS "subscription_plan_enum"`);
  }
}
