/**
 * SQLite SupervisorPolicy Repository (spec 093)
 *
 * Backed by the supervisor_policies table. Uniqueness is enforced at the
 * schema layer via the unique-(scope_type, scope_id, COALESCE(feature_id, ''))
 * index. {@link findPolicyForScope} implements the documented feature->scope
 * fallback (research decision 7).
 */
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);
};
import { injectable } from 'tsyringe';
import { toDatabase, fromDatabase, } from '../persistence/sqlite/mappers/supervisor-policy.mapper.js';
let SQLiteSupervisorPolicyRepository = class SQLiteSupervisorPolicyRepository {
    db;
    constructor(db) {
        this.db = db;
    }
    async create(policy) {
        const row = toDatabase(policy);
        const stmt = this.db.prepare(`
      INSERT INTO supervisor_policies (
        id, scope_type, scope_id, feature_id, enabled, autonomy_level,
        gate_authority_json, model_id, prompt_version,
        policy_rules_json, notification_overrides_json,
        created_at, updated_at
      ) VALUES (
        @id, @scope_type, @scope_id, @feature_id, @enabled, @autonomy_level,
        @gate_authority_json, @model_id, @prompt_version,
        @policy_rules_json, @notification_overrides_json,
        @created_at, @updated_at
      )
    `);
        stmt.run(row);
    }
    async update(policy) {
        const row = toDatabase(policy);
        const stmt = this.db.prepare(`
      UPDATE supervisor_policies SET
        scope_type = @scope_type,
        scope_id = @scope_id,
        feature_id = @feature_id,
        enabled = @enabled,
        autonomy_level = @autonomy_level,
        gate_authority_json = @gate_authority_json,
        model_id = @model_id,
        prompt_version = @prompt_version,
        policy_rules_json = @policy_rules_json,
        notification_overrides_json = @notification_overrides_json,
        updated_at = @updated_at
      WHERE id = @id
    `);
        stmt.run(row);
    }
    async delete(id) {
        this.db.prepare('DELETE FROM supervisor_policies WHERE id = ?').run(id);
    }
    async findById(id) {
        const row = this.db.prepare('SELECT * FROM supervisor_policies WHERE id = ?').get(id);
        return row ? fromDatabase(row) : null;
    }
    async findByScope(scopeType, scopeId) {
        const row = this.db
            .prepare(`SELECT * FROM supervisor_policies
         WHERE scope_type = ? AND COALESCE(scope_id, '') = COALESCE(?, '')
           AND feature_id IS NULL
         LIMIT 1`)
            .get(scopeType, scopeId ?? null);
        return row ? fromDatabase(row) : null;
    }
    async findByScopeAndFeature(scopeType, scopeId, featureId) {
        const row = this.db
            .prepare(`SELECT * FROM supervisor_policies
         WHERE scope_type = ? AND COALESCE(scope_id, '') = COALESCE(?, '')
           AND feature_id = ?
         LIMIT 1`)
            .get(scopeType, scopeId ?? null, featureId);
        return row ? fromDatabase(row) : null;
    }
    async findPolicyForScope(scopeType, scopeId, featureId) {
        if (featureId !== undefined) {
            const featureRow = await this.findByScopeAndFeature(scopeType, scopeId, featureId);
            if (featureRow)
                return featureRow;
        }
        return this.findByScope(scopeType, scopeId);
    }
    async listByScope(scopeType, scopeId) {
        const rows = this.db
            .prepare(`SELECT * FROM supervisor_policies
         WHERE scope_type = ? AND COALESCE(scope_id, '') = COALESCE(?, '')
         ORDER BY feature_id IS NULL DESC, feature_id ASC`)
            .all(scopeType, scopeId ?? null);
        return rows.map(fromDatabase);
    }
    async listAll() {
        const rows = this.db
            .prepare(`SELECT * FROM supervisor_policies
         ORDER BY scope_type ASC,
                  scope_id IS NULL DESC, scope_id ASC,
                  feature_id IS NULL DESC, feature_id ASC`)
            .all();
        return rows.map(fromDatabase);
    }
};
SQLiteSupervisorPolicyRepository = __decorate([
    injectable(),
    __metadata("design:paramtypes", [Object])
], SQLiteSupervisorPolicyRepository);
export { SQLiteSupervisorPolicyRepository };
