/**
 * SQLite GitHub Integration Repository
 *
 * Mirrors SQLiteCloudProviderTokensRepository but for a single fixed row
 * (id = 1) — there's only one GitHub integration per shep instance.
 */
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 { LocalSecretBox } from '../services/crypto/local-secret-box.js';
const ROW_ID = 1;
let SQLiteGithubIntegrationRepository = class SQLiteGithubIntegrationRepository {
    db;
    secretBox;
    constructor(db, secretBox) {
        this.db = db;
        this.secretBox = secretBox;
    }
    async get() {
        const row = this.db
            .prepare('SELECT id, token_ciphertext, token_iv, token_tag, created_at, updated_at FROM github_integration WHERE id = ?')
            .get(ROW_ID);
        if (!row)
            return null;
        return this.secretBox.decrypt({
            ciphertext: row.token_ciphertext,
            iv: row.token_iv,
            tag: row.token_tag,
        });
    }
    async set(token) {
        const now = Date.now();
        const blob = this.secretBox.encrypt(token);
        this.db
            .prepare(`INSERT INTO github_integration (
           id, token_ciphertext, token_iv, token_tag, created_at, updated_at
         ) VALUES (
           @id, @token_ciphertext, @token_iv, @token_tag, @created_at, @updated_at
         )
         ON CONFLICT(id) DO UPDATE SET
           token_ciphertext = excluded.token_ciphertext,
           token_iv = excluded.token_iv,
           token_tag = excluded.token_tag,
           updated_at = excluded.updated_at`)
            .run({
            id: ROW_ID,
            token_ciphertext: blob.ciphertext,
            token_iv: blob.iv,
            token_tag: blob.tag,
            created_at: now,
            updated_at: now,
        });
    }
    async remove() {
        this.db.prepare('DELETE FROM github_integration WHERE id = ?').run(ROW_ID);
    }
    async getStatus() {
        const row = this.db
            .prepare('SELECT created_at, updated_at FROM github_integration WHERE id = ?')
            .get(ROW_ID);
        if (!row)
            return { connected: false, connectedAt: null, updatedAt: null };
        return { connected: true, connectedAt: row.created_at, updatedAt: row.updated_at };
    }
};
SQLiteGithubIntegrationRepository = __decorate([
    injectable(),
    __metadata("design:paramtypes", [Object, LocalSecretBox])
], SQLiteGithubIntegrationRepository);
export { SQLiteGithubIntegrationRepository };
