import { logger } from '../utils/logger';
import db from '../config/database';

export default class TestDatabaseInitializer {
  static async initTables(): Promise<void> {
    await this.initSecurityTestTable();
    await this.initSecurityScanTable();
    await this.initP0CheckTable();
    await this.initRegressionTestTable();
  }

  private static async initSecurityTestTable(): Promise<void> {
    const hasTable = await db.schema.hasTable('cf_security_test');
    if (!hasTable) {
      logger.info('[TestDatabaseInitializer] Creating cf_security_test table...');
      await db.schema.createTable('cf_security_test', (table) => {
        table.string('id', 64).primary();
        table.string('target_url', 255).notNullable();
        table.datetime('start_time').notNullable();
        table.datetime('end_time').notNullable();
        table.integer('duration').notNullable();
        table.integer('total_tests').notNullable();
        table.integer('passed_tests').notNullable();
        table.integer('failed_tests').notNullable();
        table.decimal('risk_score', 5, 2).notNullable();
        table.text('summary').notNullable();
        table.json('vulnerabilities').notNullable();
        table.json('recommendations').notNullable();
        table.datetime('created_at').notNullable().defaultTo(db.fn.now());
        table.index('created_at');
      });
      logger.info('[TestDatabaseInitializer] Table cf_security_test created');
    }
  }

  private static async initSecurityScanTable(): Promise<void> {
    const hasTable = await db.schema.hasTable('cf_security_scan');
    if (!hasTable) {
      logger.info('[TestDatabaseInitializer] Creating cf_security_scan table...');
      await db.schema.createTable('cf_security_scan', (table) => {
        table.string('id', 64).primary();
        table.string('project_root', 255).notNullable();
        table.datetime('scan_time').notNullable();
        table.integer('total_dependencies').notNullable();
        table.integer('vulnerable_dependencies').notNullable();
        table.json('severity_counts').notNullable();
        table.decimal('risk_score', 5, 2).notNullable();
        table.text('summary').notNullable();
        table.json('vulnerabilities').notNullable();
        table.json('recommendations').notNullable();
        table.json('license_issues').notNullable();
        table.datetime('created_at').notNullable().defaultTo(db.fn.now());
        table.index('created_at');
      });
      logger.info('[TestDatabaseInitializer] Table cf_security_scan created');
    }
  }

  private static async initP0CheckTable(): Promise<void> {
    const hasTable = await db.schema.hasTable('cf_p0_check');
    if (!hasTable) {
      logger.info('[TestDatabaseInitializer] Creating cf_p0_check table...');
      await db.schema.createTable('cf_p0_check', (table) => {
        table.string('id', 64).primary();
        table.datetime('check_time').notNullable();
        table.integer('total_p0_tasks').notNullable();
        table.integer('completed_p0_tasks').notNullable();
        table.integer('pending_p0_tasks').notNullable();
        table.integer('failed_p0_tasks').notNullable();
        table.boolean('is_ready_for_launch').notNullable();
        table.text('summary').notNullable();
        table.json('blocking_issues').notNullable();
        table.json('recommendations').notNullable();
        table.json('tasks').notNullable();
        table.datetime('created_at').notNullable().defaultTo(db.fn.now());
        table.index('created_at');
      });
      logger.info('[TestDatabaseInitializer] Table cf_p0_check created');
    }
  }

  private static async initRegressionTestTable(): Promise<void> {
    const hasTable = await db.schema.hasTable('cf_regression_test');
    if (!hasTable) {
      logger.info('[TestDatabaseInitializer] Creating cf_regression_test table...');
      await db.schema.createTable('cf_regression_test', (table) => {
        table.string('id', 64).primary();
        table.string('test_suite', 128).notNullable();
        table.datetime('start_time').notNullable();
        table.datetime('end_time').notNullable();
        table.integer('duration').notNullable();
        table.integer('total_tests').notNullable();
        table.integer('passed_tests').notNullable();
        table.integer('failed_tests').notNullable();
        table.integer('skipped_tests').notNullable();
        table.boolean('success').notNullable();
        table.text('summary').notNullable();
        table.json('recommendations').notNullable();
        table.json('results').notNullable();
        table.string('environment', 32).notNullable();
        table.datetime('created_at').notNullable().defaultTo(db.fn.now());
        table.index('created_at');
      });
      logger.info('[TestDatabaseInitializer] Table cf_regression_test created');
    }
  }
}
