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

export class Migration006DropRegistryToken extends BaseMigration {
  readonly version = 6;
  readonly description = 'Drop registry_token column from services table';

  up(query: TQueryFunction): void {
    query(`
      CREATE TABLE services_new (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        name TEXT NOT NULL UNIQUE,
        image TEXT NOT NULL,
        registry TEXT,
        env_vars TEXT,
        port INTEGER NOT NULL,
        domain TEXT,
        container_id TEXT,
        status TEXT NOT NULL,
        created_at REAL NOT NULL,
        updated_at REAL NOT NULL,
        use_onebox_registry INTEGER DEFAULT 0,
        registry_repository TEXT,
        registry_image_tag TEXT DEFAULT 'latest',
        auto_update_on_push INTEGER DEFAULT 0,
        image_digest TEXT
      )
    `);

    query(`
      INSERT INTO services_new (
        id, name, image, registry, env_vars, port, domain, container_id, status,
        created_at, updated_at, use_onebox_registry, registry_repository,
        registry_image_tag, auto_update_on_push, image_digest
      )
      SELECT
        id, name, image, registry, env_vars, port, domain, container_id, status,
        created_at, updated_at, use_onebox_registry, registry_repository,
        registry_image_tag, auto_update_on_push, image_digest
      FROM services
    `);

    query('DROP TABLE services');
    query('ALTER TABLE services_new RENAME TO services');
    query('CREATE INDEX IF NOT EXISTS idx_services_name ON services(name)');
    query('CREATE INDEX IF NOT EXISTS idx_services_status ON services(status)');
  }
}
