import { MigrationInterface, QueryRunner } from 'typeorm';

export class InitSchema1755845112842 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    // Admin 基线表（对应 common/auth/entities/admin.entity.ts）
    await queryRunner.query(`
      CREATE TABLE IF NOT EXISTS \`wwjauth_admins\` (
        \`id\` bigint unsigned NOT NULL AUTO_INCREMENT,
        \`username\` varchar(64) NOT NULL,
        \`mobile\` varchar(32) NULL DEFAULT NULL,
        \`password_hash\` varchar(255) NOT NULL,
        \`nickname\` varchar(128) NULL,
        \`avatar\` varchar(255) NULL,
        \`login_ip\` varchar(64) NULL,
        \`type\` tinyint NOT NULL DEFAULT 1,
        \`tenant_id\` int unsigned NOT NULL DEFAULT 0,
        \`last_login_at\` datetime NULL,
        \`created_at\` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
        \`updated_at\` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        \`status\` enum('enabled','disabled') NOT NULL DEFAULT 'enabled',
        \`is_founder\` tinyint(1) NOT NULL DEFAULT 0,
        PRIMARY KEY (\`id\`),
        UNIQUE KEY \`uk_admin_username\` (\`username\`),
        UNIQUE KEY \`uk_admin_mobile\` (\`mobile\`)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
    `);

    // Member 基线表（对应 common/users/entities/member.entity.ts）
    await queryRunner.query(`
      CREATE TABLE IF NOT EXISTS \`member\` (
        \`member_id\` int unsigned NOT NULL AUTO_INCREMENT,
        \`username\` varchar(255) NOT NULL DEFAULT '',
        \`mobile\` varchar(20) NOT NULL DEFAULT '',
        \`password\` varchar(255) NOT NULL DEFAULT '',
        \`status\` tinyint NOT NULL DEFAULT 1,
        \`is_del\` tinyint NOT NULL DEFAULT 0,
        \`login_count\` int NOT NULL DEFAULT 0,
        \`login_time\` int NOT NULL DEFAULT 0,
        \`login_ip\` varchar(255) NOT NULL DEFAULT '',
        \`create_time\` int NOT NULL DEFAULT 0,
        \`update_time\` int NOT NULL DEFAULT 0,
        PRIMARY KEY (\`member_id\`)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    // 回滚顺序与创建相反
    await queryRunner.query('DROP TABLE IF EXISTS `member`;');
    await queryRunner.query('DROP TABLE IF EXISTS `wwjauth_admins`;');
  }
}
