import { MigrationInterface, QueryRunner, Table, TableForeignKey } from 'typeorm';

export class CreateInvitationTokens1732896000000 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.createTable(
      new Table({
        name: 'invitation_tokens',
        columns: [
          {
            name: 'id',
            type: 'uuid',
            isPrimary: true,
            default: 'uuid_generate_v4()',
          },
          {
            name: 'token',
            type: 'varchar',
            length: '255',
            isUnique: true,
          },
          {
            name: 'email',
            type: 'varchar',
            length: '255',
          },
          {
            name: 'first_name',
            type: 'varchar',
            length: '100',
          },
          {
            name: 'last_name',
            type: 'varchar',
            length: '100',
          },
          {
            name: 'role',
            type: 'enum',
            enum: ['ADMIN', 'MANAGER', 'USER', 'VIEWER'],
            default: "'USER'",
          },
          {
            name: 'organization_id',
            type: 'uuid',
          },
          {
            name: 'invited_by_id',
            type: 'uuid',
          },
          {
            name: 'expires_at',
            type: 'timestamp',
          },
          {
            name: 'used_at',
            type: 'timestamp',
            isNullable: true,
          },
          {
            name: 'is_used',
            type: 'boolean',
            default: false,
          },
          {
            name: 'created_at',
            type: 'timestamp',
            default: 'CURRENT_TIMESTAMP',
          },
        ],
      }),
      true
    );

    // Add foreign key for organization
    await queryRunner.createForeignKey(
      'invitation_tokens',
      new TableForeignKey({
        columnNames: ['organization_id'],
        referencedColumnNames: ['id'],
        referencedTableName: 'organizations',
        onDelete: 'CASCADE',
      })
    );

    // Add foreign key for invited_by (user who sent the invitation)
    await queryRunner.createForeignKey(
      'invitation_tokens',
      new TableForeignKey({
        columnNames: ['invited_by_id'],
        referencedColumnNames: ['id'],
        referencedTableName: 'users',
        onDelete: 'SET NULL',
      })
    );

    // Add index on token for fast lookup
    await queryRunner.query(
      `CREATE INDEX "IDX_invitation_tokens_token" ON "invitation_tokens" ("token")`
    );

    // Add index on email
    await queryRunner.query(
      `CREATE INDEX "IDX_invitation_tokens_email" ON "invitation_tokens" ("email")`
    );

    // Add index on expires_at for cleanup queries
    await queryRunner.query(
      `CREATE INDEX "IDX_invitation_tokens_expires_at" ON "invitation_tokens" ("expires_at")`
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.dropTable('invitation_tokens');
  }
}
