/**
 * MigrationStatusDiagnostic
 *
 * Reports whether the local SQLite database has at least one applied
 * migration. The DI container runs migrations on bootstrap, so an empty
 * `umzug_migrations` table generally means migrations failed silently or
 * the schema is older than the running code expects.
 */
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
import { inject, injectable } from 'tsyringe';
import { DiagnosticStatus } from '../../../../domain/generated/output.js';
let MigrationStatusDiagnostic = class MigrationStatusDiagnostic {
    db;
    name = 'migration-status';
    constructor(db) {
        this.db = db;
    }
    async run() {
        try {
            const row = this.db
                .prepare("SELECT COUNT(*) AS count FROM sqlite_master WHERE type = 'table' AND name = 'umzug_migrations'")
                .get();
            if (row.count === 0) {
                return {
                    name: this.name,
                    status: DiagnosticStatus.Fail,
                    detail: 'No `umzug_migrations` table — migrations have not been bootstrapped',
                    fixHint: 'Re-run `pnpm dev:cli` so the DI container can run migrations on startup',
                };
            }
            const counted = this.db.prepare('SELECT COUNT(*) AS count FROM umzug_migrations').get();
            if (counted.count === 0) {
                return {
                    name: this.name,
                    status: DiagnosticStatus.Fail,
                    detail: 'umzug_migrations table is empty — schema is not initialized',
                    fixHint: 'Re-run `pnpm dev:cli` to apply migrations',
                };
            }
            return {
                name: this.name,
                status: DiagnosticStatus.Ok,
                detail: `${counted.count} migration(s) applied`,
            };
        }
        catch (err) {
            return {
                name: this.name,
                status: DiagnosticStatus.Fail,
                detail: `Could not read migration status: ${err instanceof Error ? err.message : String(err)}`,
                fixHint: 'Check that the SQLite database is reachable and not corrupted',
            };
        }
    }
};
MigrationStatusDiagnostic = __decorate([
    injectable(),
    __param(0, inject('Database')),
    __metadata("design:paramtypes", [Object])
], MigrationStatusDiagnostic);
export { MigrationStatusDiagnostic };
