import { MigrationInterface, QueryRunner } from "typeorm";

export class AddTenantModule1772334811108 implements MigrationInterface {

    public async up(queryRunner: QueryRunner): Promise<void> {
        // 1. Ensure 'tenants' table exists before we insert into it. 
        // This assumes the schema definition migrations have run or TypeORM synchronize handled the tables.
        // We will insert a system default tenant.

        await queryRunner.query(`
            INSERT INTO "tenants" ("id", "name", "description", "isActive", "created_at", "updated_at")
            SELECT '00000000-0000-0000-0000-000000000000', 'Default Tenant', 'System Default Organization', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
            WHERE NOT EXISTS (SELECT 1 FROM "tenants" WHERE "id" = '00000000-0000-0000-0000-000000000000');
        `);

        // 2. Link existing users to the default tenant
        await queryRunner.query(`UPDATE "users" SET "tenant_id" = '00000000-0000-0000-0000-000000000000' WHERE "tenant_id" IS NULL;`);

        // 3. Link existing knowledge bases to the default tenant
        await queryRunner.query(`UPDATE "knowledge_bases" SET "tenant_id" = '00000000-0000-0000-0000-000000000000' WHERE "tenant_id" IS NULL;`);

        // 4. Link existing knowledge groups to the default tenant
        await queryRunner.query(`UPDATE "knowledge_groups" SET "tenant_id" = '00000000-0000-0000-0000-000000000000' WHERE "tenant_id" IS NULL;`);

        // 5. Link existing search histories to the default tenant
        await queryRunner.query(`UPDATE "search_history" SET "tenant_id" = '00000000-0000-0000-0000-000000000000' WHERE "tenant_id" IS NULL;`);

        // 6. Link existing notes to the default tenant
        await queryRunner.query(`UPDATE "notes" SET "tenant_id" = '00000000-0000-0000-0000-000000000000' WHERE "tenant_id" IS NULL;`);

        // 7. Make the existing admin users SUPER_ADMIN
        await queryRunner.query(`UPDATE "users" SET "role" = 'SUPER_ADMIN' WHERE "isAdmin" = 1;`);
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        // Reverse operations if needed. Note: We do not delete the Default tenant here because it might be in active use.
        // But we could nullify the links if we were strictly rolling back to a state without tenant links.
        /*
        await queryRunner.query(`UPDATE "users" SET "tenant_id" = NULL WHERE "tenant_id" = '00000000-0000-0000-0000-000000000000';`);
        await queryRunner.query(`UPDATE "knowledge_bases" SET "tenant_id" = NULL WHERE "tenant_id" = '00000000-0000-0000-0000-000000000000';`);
        await queryRunner.query(`UPDATE "knowledge_groups" SET "tenant_id" = NULL WHERE "tenant_id" = '00000000-0000-0000-0000-000000000000';`);
        await queryRunner.query(`UPDATE "search_history" SET "tenant_id" = NULL WHERE "tenant_id" = '00000000-0000-0000-0000-000000000000';`);
        await queryRunner.query(`UPDATE "notes" SET "tenant_id" = NULL WHERE "tenant_id" = '00000000-0000-0000-0000-000000000000';`);
        */
    }

}
