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

/**
 * Migration: Create YtSeries Collection
 *
 * Creates the yt_series collection for managing YouTube channel content series
 * with branding, descriptions, and YouTube playlist integration.
 */
export async function up({ db }: MigrateUpArgs): Promise<void> {
  await db.execute(sql`
    -- =====================================================
    -- YT_SERIES COLLECTION
    -- =====================================================

    CREATE TABLE IF NOT EXISTS "yt_series" (
      "id" serial PRIMARY KEY NOT NULL,
      "slug" varchar NOT NULL,
      "channel_id" integer REFERENCES youtube_channels(id) ON DELETE CASCADE,
      "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,
      "format" varchar,
      "publishing_frequency" varchar,
      "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 "yt_series_slug_channel_unique" UNIQUE("slug", "channel_id")
    );

    -- Localized fields for YtSeries (name, description)
    CREATE TABLE IF NOT EXISTS "yt_series_locales" (
      "name" varchar NOT NULL,
      "description" text,
      "id" serial PRIMARY KEY NOT NULL,
      "_locale" varchar NOT NULL,
      "_parent_id" integer NOT NULL REFERENCES yt_series(id) ON DELETE CASCADE,
      CONSTRAINT "yt_series_locales_locale_parent_id_unique" UNIQUE("_locale", "_parent_id")
    );

    -- Relationships table
    CREATE TABLE IF NOT EXISTS "yt_series_rels" (
      "id" serial PRIMARY KEY NOT NULL,
      "order" integer,
      "parent_id" integer NOT NULL REFERENCES yt_series(id) ON DELETE CASCADE,
      "path" varchar NOT NULL,
      "youtube_channels_id" integer REFERENCES youtube_channels(id) ON DELETE CASCADE,
      "media_id" integer REFERENCES media(id) ON DELETE CASCADE
    );

    -- Indexes for yt_series
    CREATE INDEX IF NOT EXISTS "yt_series_slug_idx" ON "yt_series" USING btree ("slug");
    CREATE INDEX IF NOT EXISTS "yt_series_channel_idx" ON "yt_series" USING btree ("channel_id");
    CREATE INDEX IF NOT EXISTS "yt_series_logo_idx" ON "yt_series" USING btree ("logo_id");
    CREATE INDEX IF NOT EXISTS "yt_series_cover_image_idx" ON "yt_series" USING btree ("cover_image_id");
    CREATE INDEX IF NOT EXISTS "yt_series_format_idx" ON "yt_series" USING btree ("format");
    CREATE INDEX IF NOT EXISTS "yt_series_is_active_idx" ON "yt_series" USING btree ("is_active");
    CREATE INDEX IF NOT EXISTS "yt_series_updated_at_idx" ON "yt_series" USING btree ("updated_at");
    CREATE INDEX IF NOT EXISTS "yt_series_created_at_idx" ON "yt_series" USING btree ("created_at");

    -- Indexes for yt_series_locales
    CREATE INDEX IF NOT EXISTS "yt_series_locales_locale_idx" ON "yt_series_locales" USING btree ("_locale");
    CREATE INDEX IF NOT EXISTS "yt_series_locales_parent_idx" ON "yt_series_locales" USING btree ("_parent_id");

    -- Indexes for yt_series_rels
    CREATE INDEX IF NOT EXISTS "yt_series_rels_order_idx" ON "yt_series_rels" USING btree ("order");
    CREATE INDEX IF NOT EXISTS "yt_series_rels_parent_idx" ON "yt_series_rels" USING btree ("parent_id");
    CREATE INDEX IF NOT EXISTS "yt_series_rels_path_idx" ON "yt_series_rels" USING btree ("path");
    CREATE INDEX IF NOT EXISTS "yt_series_rels_youtube_channels_idx" ON "yt_series_rels" USING btree ("youtube_channels_id");
    CREATE INDEX IF NOT EXISTS "yt_series_rels_media_idx" ON "yt_series_rels" USING btree ("media_id");

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

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

    CREATE INDEX IF NOT EXISTS "payload_locked_documents_rels_yt_series_idx"
      ON "payload_locked_documents_rels" USING btree ("yt_series_id");

    -- =====================================================
    -- ADD series_id TO youtube_content
    -- For the new relationship field
    -- =====================================================

    ALTER TABLE "youtube_content"
      ADD COLUMN IF NOT EXISTS "series_id" integer REFERENCES yt_series(id) ON DELETE SET NULL;

    CREATE INDEX IF NOT EXISTS "youtube_content_series_idx"
      ON "youtube_content" USING btree ("series_id");
  `)
}

export async function down({ db }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
    -- Remove youtube_content.series_id
    DROP INDEX IF EXISTS "youtube_content_series_idx";
    ALTER TABLE "youtube_content" DROP COLUMN IF EXISTS "series_id";

    -- Remove from system table
    DROP INDEX IF EXISTS "payload_locked_documents_rels_yt_series_idx";
    ALTER TABLE "payload_locked_documents_rels" DROP COLUMN IF EXISTS "yt_series_id";

    -- Drop yt_series tables
    DROP TABLE IF EXISTS "yt_series_rels";
    DROP TABLE IF EXISTS "yt_series_locales";
    DROP TABLE IF EXISTS "yt_series";
  `)
}
