import { MigrationInterface, QueryRunner } from 'typeorm';

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

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      CREATE TABLE IF NOT EXISTS "server_plugin_requirements" (
        "serverId"     TEXT NOT NULL,
        "pluginId"     TEXT NOT NULL,
        "status"       TEXT NOT NULL,
        "versionRange" TEXT,
        "reason"       TEXT,
        "configuredBy" TEXT,
        "createdAt"    INTEGER NOT NULL,
        "updatedAt"    INTEGER NOT NULL,
        PRIMARY KEY ("serverId", "pluginId")
      )
    `);
    await queryRunner.query(`
      CREATE INDEX IF NOT EXISTS "idx_server_plugin_requirements_status"
      ON "server_plugin_requirements" ("status")
    `);

    await queryRunner.query(`
      CREATE TABLE IF NOT EXISTS "server_plugin_event_definitions" (
        "serverId"        TEXT NOT NULL,
        "pluginId"        TEXT NOT NULL,
        "eventName"       TEXT NOT NULL,
        "direction"       TEXT NOT NULL,
        "scope"           TEXT NOT NULL,
        "schemaJson"      TEXT,
        "maxPayloadBytes" INTEGER NOT NULL,
        "rateLimitJson"   TEXT,
        "createdAt"       INTEGER NOT NULL,
        "updatedAt"       INTEGER NOT NULL,
        PRIMARY KEY ("serverId", "pluginId", "eventName")
      )
    `);

    await queryRunner.query(`
      CREATE TABLE IF NOT EXISTS "plugin_data" (
        "serverId"      TEXT NOT NULL,
        "pluginId"      TEXT NOT NULL,
        "scope"         TEXT NOT NULL,
        "ownerId"       TEXT NOT NULL,
        "key"           TEXT NOT NULL,
        "valueJson"     TEXT NOT NULL,
        "schemaVersion" INTEGER NOT NULL DEFAULT 1,
        "updatedBy"     TEXT,
        "updatedAt"     INTEGER NOT NULL,
        PRIMARY KEY ("serverId", "pluginId", "scope", "ownerId", "key")
      )
    `);

    await queryRunner.query(`
      CREATE TABLE IF NOT EXISTS "server_plugin_settings" (
        "serverId"      TEXT NOT NULL,
        "pluginId"      TEXT NOT NULL,
        "settingsJson"  TEXT NOT NULL,
        "schemaVersion" INTEGER NOT NULL DEFAULT 1,
        "updatedBy"     TEXT,
        "updatedAt"     INTEGER NOT NULL,
        PRIMARY KEY ("serverId", "pluginId")
      )
    `);

    await queryRunner.query(`
      CREATE TABLE IF NOT EXISTS "plugin_user_metadata" (
        "serverId"        TEXT NOT NULL,
        "pluginId"        TEXT NOT NULL,
        "pluginUserId"    TEXT NOT NULL,
        "displayName"     TEXT NOT NULL,
        "avatarHash"      TEXT,
        "avatarMime"      TEXT,
        "avatarUpdatedAt" INTEGER,
        "roleIdsJson"     TEXT NOT NULL,
        "createdAt"       INTEGER NOT NULL,
        "updatedAt"       INTEGER NOT NULL,
        PRIMARY KEY ("serverId", "pluginId", "pluginUserId")
      )
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`DROP TABLE IF EXISTS "plugin_user_metadata"`);
    await queryRunner.query(`DROP TABLE IF EXISTS "server_plugin_settings"`);
    await queryRunner.query(`DROP TABLE IF EXISTS "plugin_data"`);
    await queryRunner.query(`DROP TABLE IF EXISTS "server_plugin_event_definitions"`);
    await queryRunner.query(`DROP TABLE IF EXISTS "server_plugin_requirements"`);
  }
}
