import { MigrationInterface, QueryRunner } from 'typeorm';

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

  public async up(queryRunner: QueryRunner): Promise<void> {
    // Create push_subscriptions table
    await queryRunner.query(`
      CREATE TABLE push_subscriptions (
        id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
        user_id VARCHAR(20) NOT NULL REFERENCES users(id) ON DELETE CASCADE,
        endpoint TEXT NOT NULL,
        p256dh TEXT NOT NULL,
        auth TEXT NOT NULL,
        user_agent TEXT,
        device_type VARCHAR(20),
        browser VARCHAR(50),
        is_active BOOLEAN DEFAULT true,
        last_error TEXT,
        failed_attempts INTEGER DEFAULT 0,
        last_success_at TIMESTAMP,
        created_at TIMESTAMP DEFAULT NOW(),
        updated_at TIMESTAMP DEFAULT NOW(),
        CONSTRAINT unique_endpoint UNIQUE(endpoint)
      );
    `);

    // Create indexes for push_subscriptions
    await queryRunner.query(`
      CREATE INDEX idx_push_subs_user_id ON push_subscriptions(user_id);
    `);

    await queryRunner.query(`
      CREATE INDEX idx_push_subs_active ON push_subscriptions(is_active) WHERE is_active = true;
    `);

    // Create notification_queue table
    await queryRunner.query(`
      CREATE TABLE notification_queue (
        id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
        user_id VARCHAR(20) NOT NULL REFERENCES users(id) ON DELETE CASCADE,
        notification_type VARCHAR(50) NOT NULL,
        title VARCHAR(255) NOT NULL,
        body TEXT NOT NULL,
        icon_url TEXT,
        badge_url TEXT,
        action_url TEXT,
        data JSONB,
        priority VARCHAR(20) DEFAULT 'normal',
        status VARCHAR(20) DEFAULT 'pending',
        scheduled_at TIMESTAMP DEFAULT NOW(),
        sent_at TIMESTAMP,
        expires_at TIMESTAMP,
        error_message TEXT,
        retry_count INTEGER DEFAULT 0,
        created_at TIMESTAMP DEFAULT NOW()
      );
    `);

    // Create indexes for notification_queue
    await queryRunner.query(`
      CREATE INDEX idx_notif_queue_status ON notification_queue(status) WHERE status = 'pending';
    `);

    await queryRunner.query(`
      CREATE INDEX idx_notif_queue_user ON notification_queue(user_id);
    `);

    await queryRunner.query(`
      CREATE INDEX idx_notif_queue_scheduled ON notification_queue(scheduled_at);
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    // Drop indexes
    await queryRunner.query(`DROP INDEX IF EXISTS idx_notif_queue_scheduled;`);
    await queryRunner.query(`DROP INDEX IF EXISTS idx_notif_queue_user;`);
    await queryRunner.query(`DROP INDEX IF EXISTS idx_notif_queue_status;`);
    await queryRunner.query(`DROP INDEX IF EXISTS idx_push_subs_active;`);
    await queryRunner.query(`DROP INDEX IF EXISTS idx_push_subs_user_id;`);

    // Drop tables
    await queryRunner.query(`DROP TABLE IF EXISTS notification_queue;`);
    await queryRunner.query(`DROP TABLE IF EXISTS push_subscriptions;`);
  }
}
