import { MigrationInterface, QueryRunner } from 'typeorm';

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

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      CREATE TABLE IF NOT EXISTS \`tetard_coins\` (
        \`id\`         VARCHAR(36)  NOT NULL,
        \`user_id\`    VARCHAR(255) NOT NULL,
        \`balance\`    INT          NOT NULL DEFAULT 0,
        \`updated_at\` DATETIME(6)  NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
        PRIMARY KEY (\`id\`),
        UNIQUE INDEX \`IDX_tetard_coins_user_id\` (\`user_id\`)
      ) ENGINE=InnoDB
    `);

    await queryRunner.query(`
      CREATE TABLE IF NOT EXISTS \`tc_transactions\` (
        \`id\`         VARCHAR(36)  NOT NULL,
        \`user_id\`    VARCHAR(255) NOT NULL,
        \`amount\`     INT          NOT NULL,
        \`reason\`     VARCHAR(100) NOT NULL,
        \`created_at\` DATETIME(6)  NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
        PRIMARY KEY (\`id\`),
        INDEX \`IDX_tc_transactions_user_id\` (\`user_id\`)
      ) ENGINE=InnoDB
    `);

    await queryRunner.query(`
      CREATE TABLE IF NOT EXISTS \`processed_events\` (
        \`id\`           VARCHAR(255) NOT NULL,
        \`processed_at\` DATETIME(6)  NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
        PRIMARY KEY (\`id\`)
      ) ENGINE=InnoDB
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`DROP TABLE IF EXISTS \`processed_events\``);
    await queryRunner.query(`DROP TABLE IF EXISTS \`tc_transactions\``);
    await queryRunner.query(`DROP TABLE IF EXISTS \`tetard_coins\``);
  }
}
