import { MigrateUpArgs, MigrateDownArgs, sql } from '@payloadcms/db-postgres'

/**
 * Migration: Add BlogWoman Collections (Favorites, Series)
 *
 * This migration creates:
 * - favorites table with _rels table for multi-tenant support
 * - series table with _locales and _rels tables
 * - Adds columns to payload_locked_documents_rels for both collections
 */
export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
  await db.execute(sql`
    -- =====================================================
    -- FAVORITES COLLECTION
    -- =====================================================

    CREATE TABLE IF NOT EXISTS "favorites" (
      "id" serial PRIMARY KEY NOT NULL,
      "title" varchar NOT NULL,
      "slug" varchar NOT NULL,
      "description" varchar,
      "category" varchar NOT NULL,
      "subcategory" varchar,
      "price" numeric,
      "price_range" varchar,
      "affiliate_url" varchar NOT NULL,
      "affiliate_network" varchar,
      "image_id" integer REFERENCES media(id) ON DELETE SET NULL,
      "badge" varchar,
      "featured" boolean DEFAULT false,
      "is_active" boolean DEFAULT true NOT NULL,
      "order" numeric DEFAULT 0,
      "updated_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
      "created_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
      CONSTRAINT "favorites_slug_unique" UNIQUE("slug")
    );

    CREATE TABLE IF NOT EXISTS "favorites_rels" (
      "id" serial PRIMARY KEY NOT NULL,
      "order" integer,
      "parent_id" integer NOT NULL REFERENCES favorites(id) ON DELETE CASCADE,
      "path" varchar NOT NULL,
      "tenants_id" integer REFERENCES tenants(id) ON DELETE CASCADE
    );

    CREATE INDEX IF NOT EXISTS "favorites_image_idx" ON "favorites" USING btree ("image_id");
    CREATE INDEX IF NOT EXISTS "favorites_slug_idx" ON "favorites" USING btree ("slug");
    CREATE INDEX IF NOT EXISTS "favorites_category_idx" ON "favorites" USING btree ("category");
    CREATE INDEX IF NOT EXISTS "favorites_updated_at_idx" ON "favorites" USING btree ("updated_at");
    CREATE INDEX IF NOT EXISTS "favorites_created_at_idx" ON "favorites" USING btree ("created_at");

    CREATE INDEX IF NOT EXISTS "favorites_rels_order_idx" ON "favorites_rels" USING btree ("order");
    CREATE INDEX IF NOT EXISTS "favorites_rels_parent_idx" ON "favorites_rels" USING btree ("parent_id");
    CREATE INDEX IF NOT EXISTS "favorites_rels_path_idx" ON "favorites_rels" USING btree ("path");
    CREATE INDEX IF NOT EXISTS "favorites_rels_tenants_idx" ON "favorites_rels" USING btree ("tenants_id");

    -- =====================================================
    -- SERIES COLLECTION
    -- =====================================================

    CREATE TABLE IF NOT EXISTS "series" (
      "id" serial PRIMARY KEY NOT NULL,
      "slug" varchar NOT NULL,
      "logo_id" integer REFERENCES media(id) ON DELETE SET NULL,
      "cover_image_id" integer REFERENCES media(id) ON DELETE SET NULL,
      "brand_color" varchar,
      "accent_color" varchar,
      "youtube_playlist_id" varchar,
      "youtube_playlist_url" varchar,
      "order" numeric DEFAULT 0,
      "is_active" boolean DEFAULT true NOT NULL,
      "updated_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
      "created_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
      CONSTRAINT "series_slug_unique" UNIQUE("slug")
    );

    -- Localized fields for Series
    CREATE TABLE IF NOT EXISTS "series_locales" (
      "title" varchar NOT NULL,
      "tagline" varchar,
      "description" jsonb,
      "id" serial PRIMARY KEY NOT NULL,
      "_locale" varchar NOT NULL,
      "_parent_id" integer NOT NULL REFERENCES series(id) ON DELETE CASCADE,
      CONSTRAINT "series_locales_locale_parent_id_unique" UNIQUE("_locale", "_parent_id")
    );

    CREATE TABLE IF NOT EXISTS "series_rels" (
      "id" serial PRIMARY KEY NOT NULL,
      "order" integer,
      "parent_id" integer NOT NULL REFERENCES series(id) ON DELETE CASCADE,
      "path" varchar NOT NULL,
      "tenants_id" integer REFERENCES tenants(id) ON DELETE CASCADE
    );

    CREATE INDEX IF NOT EXISTS "series_slug_idx" ON "series" USING btree ("slug");
    CREATE INDEX IF NOT EXISTS "series_logo_idx" ON "series" USING btree ("logo_id");
    CREATE INDEX IF NOT EXISTS "series_cover_image_idx" ON "series" USING btree ("cover_image_id");
    CREATE INDEX IF NOT EXISTS "series_updated_at_idx" ON "series" USING btree ("updated_at");
    CREATE INDEX IF NOT EXISTS "series_created_at_idx" ON "series" USING btree ("created_at");

    CREATE INDEX IF NOT EXISTS "series_locales_locale_idx" ON "series_locales" USING btree ("_locale");
    CREATE INDEX IF NOT EXISTS "series_locales_parent_idx" ON "series_locales" USING btree ("_parent_id");

    CREATE INDEX IF NOT EXISTS "series_rels_order_idx" ON "series_rels" USING btree ("order");
    CREATE INDEX IF NOT EXISTS "series_rels_parent_idx" ON "series_rels" USING btree ("parent_id");
    CREATE INDEX IF NOT EXISTS "series_rels_path_idx" ON "series_rels" USING btree ("path");
    CREATE INDEX IF NOT EXISTS "series_rels_tenants_idx" ON "series_rels" USING btree ("tenants_id");

    -- =====================================================
    -- SYSTEM TABLE UPDATES
    -- Add columns to payload_locked_documents_rels
    -- =====================================================

    ALTER TABLE "payload_locked_documents_rels"
      ADD COLUMN IF NOT EXISTS "favorites_id" integer REFERENCES favorites(id) ON DELETE CASCADE;

    ALTER TABLE "payload_locked_documents_rels"
      ADD COLUMN IF NOT EXISTS "series_id" integer REFERENCES series(id) ON DELETE CASCADE;

    CREATE INDEX IF NOT EXISTS "payload_locked_documents_rels_favorites_idx"
      ON "payload_locked_documents_rels" USING btree ("favorites_id");

    CREATE INDEX IF NOT EXISTS "payload_locked_documents_rels_series_idx"
      ON "payload_locked_documents_rels" USING btree ("series_id");
  `)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
    -- Remove indexes first
    DROP INDEX IF EXISTS "payload_locked_documents_rels_favorites_idx";
    DROP INDEX IF EXISTS "payload_locked_documents_rels_series_idx";

    -- Remove columns from system table
    ALTER TABLE "payload_locked_documents_rels" DROP COLUMN IF EXISTS "favorites_id";
    ALTER TABLE "payload_locked_documents_rels" DROP COLUMN IF EXISTS "series_id";

    -- Drop Series tables
    DROP TABLE IF EXISTS "series_rels";
    DROP TABLE IF EXISTS "series_locales";
    DROP TABLE IF EXISTS "series";

    -- Drop Favorites tables
    DROP TABLE IF EXISTS "favorites_rels";
    DROP TABLE IF EXISTS "favorites";
  `)
}
