import { Kysely, sql } from 'kysely'
import { addDefaultColumns } from '../db';

export async function up(db: Kysely<any>): Promise<void> {
    // TABLE: PRODUCT_SYNCS
    await db.schema.createTable("product_syncs")
        .$call(addDefaultColumns)
        .addColumn("session_id", "uuid", (cb) => cb.notNull())
        .addColumn("session_name", "text", (cb) => cb.notNull())
        .addColumn("p_product_id_filter", sql`integer[]`)
        .addColumn("syncing_p_product_ids", sql`integer[]`, (cb) => cb
            .notNull()
            .defaultTo(sql`'{}'::integer[]`)
        )
        .addColumn("started_at", "timestamptz")
        .addColumn("ended_at", "timestamptz")
        .addColumn("has_failed", "boolean", (cb) => cb.notNull().defaultTo(false))
        .execute()

    // INDEX: ONE_ACTIVE_PRODUCT_SYNC
    await db.schema.createIndex("idx_one_active_product_sync")
        .on("product_syncs")
        .column("ended_at")
        .unique()
        .where(sql.ref("started_at"), "is not", null)
        .where("ended_at", "is", null)
        .nullsNotDistinct()
        .execute();

    // TABLE: STANDARDS_DATASETS
    await db.schema.createTable("standards_datasets")
        .$call(addDefaultColumns)
        .addColumn("name", "text", (cb) => cb.notNull().defaultTo(""))
        .addColumn("data", "jsonb", (cb) => cb.notNull())
        .execute();

    // TABLE: STANDARDS_CONFIGS
    await db.schema.createTable("standards_configs")
        .$call(addDefaultColumns)
        .addColumn("name", "text", (cb) => cb.notNull().defaultTo(""))
        .addColumn("params", "jsonb", (cb) => cb.notNull())
        .addColumn("dataset_id", "bigint", (cb) => cb.notNull())
        .addForeignKeyConstraint(
            "fk_standards_configs_dataset_id",
            ["dataset_id"],
            "standards_datasets",
            ["id"],
            (cb) => cb.onDelete("restrict")
        )
        .execute();

    // TABLE: CALCULATORS
    await db.schema.createTable("calculators")
        .$call(addDefaultColumns)
        .addColumn("name", "text", (cb) => cb.notNull().unique())
        .addColumn("standards_config_id", "bigint", (cb) => cb.notNull())
        .addForeignKeyConstraint(
            "fk_calculators_standards_config_id",
            ["standards_config_id"],
            "standards_configs",
            ["id"],
            (cb) => cb.onDelete("restrict")
        )
        .execute();
}

export async function down(db: Kysely<any>): Promise<void> {
    // TABLE: PRODUCT_SYNCS
    await db.schema.dropTable("product_syncs").ifExists().execute()

    // INDEX: ONE_ACTIVE_PRODUCT_SYNC
    await db.schema.dropIndex("idx_one_active_product_sync").ifExists().execute();

    // TABLE: CALCULATORS
    await db.schema.dropTable("calculators").ifExists().execute()

    // TABLE: STANDARDS_CONFIGS
    await db.schema.dropTable("standards_configs").ifExists().execute()

    // TABLE: STANDARDS_DATASETS
    await db.schema.dropTable("standards_datasets").ifExists().execute()
}
