import { BaseMigration } from './base-migration.ts';
import type { TQueryFunction } from '../types.ts';

export class Migration002TimestampsToReal extends BaseMigration {
  readonly version = 2;
  readonly description = 'Convert timestamp columns from INTEGER to REAL';

  up(query: TQueryFunction): void {
    // SSL certificates
    query(`
      CREATE TABLE ssl_certificates_new (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        domain TEXT NOT NULL UNIQUE,
        cert_path TEXT NOT NULL,
        key_path TEXT NOT NULL,
        full_chain_path TEXT NOT NULL,
        expiry_date REAL NOT NULL,
        issuer TEXT NOT NULL,
        created_at REAL NOT NULL,
        updated_at REAL NOT NULL
      )
    `);
    query(`INSERT INTO ssl_certificates_new SELECT * FROM ssl_certificates`);
    query(`DROP TABLE ssl_certificates`);
    query(`ALTER TABLE ssl_certificates_new RENAME TO ssl_certificates`);

    // Services
    query(`
      CREATE TABLE services_new (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        name TEXT NOT NULL UNIQUE,
        image TEXT NOT NULL,
        registry TEXT,
        env_vars TEXT NOT NULL,
        port INTEGER NOT NULL,
        domain TEXT,
        container_id TEXT,
        status TEXT NOT NULL DEFAULT 'stopped',
        created_at REAL NOT NULL,
        updated_at REAL NOT NULL
      )
    `);
    query(`INSERT INTO services_new SELECT * FROM services`);
    query(`DROP TABLE services`);
    query(`ALTER TABLE services_new RENAME TO services`);

    // Registries
    query(`
      CREATE TABLE registries_new (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        url TEXT NOT NULL UNIQUE,
        username TEXT NOT NULL,
        password_encrypted TEXT NOT NULL,
        created_at REAL NOT NULL
      )
    `);
    query(`INSERT INTO registries_new SELECT * FROM registries`);
    query(`DROP TABLE registries`);
    query(`ALTER TABLE registries_new RENAME TO registries`);

    // Nginx configs
    query(`
      CREATE TABLE nginx_configs_new (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        service_id INTEGER NOT NULL,
        domain TEXT NOT NULL,
        port INTEGER NOT NULL,
        ssl_enabled INTEGER NOT NULL DEFAULT 0,
        config_template TEXT NOT NULL,
        created_at REAL NOT NULL,
        updated_at REAL NOT NULL,
        FOREIGN KEY (service_id) REFERENCES services(id) ON DELETE CASCADE
      )
    `);
    query(`INSERT INTO nginx_configs_new SELECT * FROM nginx_configs`);
    query(`DROP TABLE nginx_configs`);
    query(`ALTER TABLE nginx_configs_new RENAME TO nginx_configs`);

    // DNS records
    query(`
      CREATE TABLE dns_records_new (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        domain TEXT NOT NULL UNIQUE,
        type TEXT NOT NULL,
        value TEXT NOT NULL,
        cloudflare_id TEXT,
        zone_id TEXT,
        created_at REAL NOT NULL,
        updated_at REAL NOT NULL
      )
    `);
    query(`INSERT INTO dns_records_new SELECT * FROM dns_records`);
    query(`DROP TABLE dns_records`);
    query(`ALTER TABLE dns_records_new RENAME TO dns_records`);

    // Metrics
    query(`
      CREATE TABLE metrics_new (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        service_id INTEGER NOT NULL,
        timestamp REAL NOT NULL,
        cpu_percent REAL NOT NULL,
        memory_used INTEGER NOT NULL,
        memory_limit INTEGER NOT NULL,
        network_rx_bytes INTEGER NOT NULL,
        network_tx_bytes INTEGER NOT NULL,
        FOREIGN KEY (service_id) REFERENCES services(id) ON DELETE CASCADE
      )
    `);
    query(`INSERT INTO metrics_new SELECT * FROM metrics`);
    query(`DROP TABLE metrics`);
    query(`ALTER TABLE metrics_new RENAME TO metrics`);
    query(`CREATE INDEX IF NOT EXISTS idx_metrics_service_timestamp ON metrics(service_id, timestamp DESC)`);

    // Logs
    query(`
      CREATE TABLE logs_new (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        service_id INTEGER NOT NULL,
        timestamp REAL NOT NULL,
        message TEXT NOT NULL,
        level TEXT NOT NULL,
        source TEXT NOT NULL,
        FOREIGN KEY (service_id) REFERENCES services(id) ON DELETE CASCADE
      )
    `);
    query(`INSERT INTO logs_new SELECT * FROM logs`);
    query(`DROP TABLE logs`);
    query(`ALTER TABLE logs_new RENAME TO logs`);
    query(`CREATE INDEX IF NOT EXISTS idx_logs_service_timestamp ON logs(service_id, timestamp DESC)`);

    // Users
    query(`
      CREATE TABLE users_new (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        username TEXT NOT NULL UNIQUE,
        password_hash TEXT NOT NULL,
        role TEXT NOT NULL DEFAULT 'user',
        created_at REAL NOT NULL,
        updated_at REAL NOT NULL
      )
    `);
    query(`INSERT INTO users_new SELECT * FROM users`);
    query(`DROP TABLE users`);
    query(`ALTER TABLE users_new RENAME TO users`);

    // Settings
    query(`
      CREATE TABLE settings_new (
        key TEXT PRIMARY KEY,
        value TEXT NOT NULL,
        updated_at REAL NOT NULL
      )
    `);
    query(`INSERT INTO settings_new SELECT * FROM settings`);
    query(`DROP TABLE settings`);
    query(`ALTER TABLE settings_new RENAME TO settings`);

    // Migrations table itself
    query(`
      CREATE TABLE migrations_new (
        version INTEGER PRIMARY KEY,
        applied_at REAL NOT NULL
      )
    `);
    query(`INSERT INTO migrations_new SELECT * FROM migrations`);
    query(`DROP TABLE migrations`);
    query(`ALTER TABLE migrations_new RENAME TO migrations`);
  }
}
