/**
 * SQLite CodeReview Repository Implementation
 *
 * Implements ICodeReviewRepository using better-sqlite3.
 * Stores review comments as JSON in a TEXT column.
 */
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/code-review.mapper.js';
let SQLiteCodeReviewRepository = class SQLiteCodeReviewRepository {
    db;
    constructor(db) {
        this.db = db;
    }
    async create(review) {
        const row = toDatabase(review);
        const stmt = this.db.prepare(`
      INSERT INTO code_reviews (
        id, feature_id, repository_path, pr_number, pr_url,
        status, summary, comments, review_url,
        agent_model, token_usage, error_message,
        created_at, updated_at
      ) VALUES (
        @id, @feature_id, @repository_path, @pr_number, @pr_url,
        @status, @summary, @comments, @review_url,
        @agent_model, @token_usage, @error_message,
        @created_at, @updated_at
      )
    `);
        stmt.run(row);
    }
    async update(review) {
        const row = toDatabase(review);
        const stmt = this.db.prepare(`
      UPDATE code_reviews SET
        feature_id = @feature_id,
        repository_path = @repository_path,
        pr_number = @pr_number,
        pr_url = @pr_url,
        status = @status,
        summary = @summary,
        comments = @comments,
        review_url = @review_url,
        agent_model = @agent_model,
        token_usage = @token_usage,
        error_message = @error_message,
        updated_at = @updated_at
      WHERE id = @id
    `);
        stmt.run(row);
    }
    async findById(id) {
        const stmt = this.db.prepare('SELECT * FROM code_reviews WHERE id = ?');
        const row = stmt.get(id);
        return row ? fromDatabase(row) : null;
    }
    async findByFeatureId(featureId) {
        const stmt = this.db.prepare('SELECT * FROM code_reviews WHERE feature_id = ? ORDER BY created_at DESC');
        const rows = stmt.all(featureId);
        return rows.map(fromDatabase);
    }
    async findByPrNumber(repositoryPath, prNumber) {
        const stmt = this.db.prepare('SELECT * FROM code_reviews WHERE repository_path = ? AND pr_number = ? ORDER BY created_at DESC');
        const rows = stmt.all(repositoryPath, prNumber);
        return rows.map(fromDatabase);
    }
    async list(repositoryPath, options) {
        const limit = options?.limit ?? 50;
        if (repositoryPath) {
            const stmt = this.db.prepare('SELECT * FROM code_reviews WHERE repository_path = ? ORDER BY created_at DESC LIMIT ?');
            const rows = stmt.all(repositoryPath, limit);
            return rows.map(fromDatabase);
        }
        const stmt = this.db.prepare('SELECT * FROM code_reviews ORDER BY created_at DESC LIMIT ?');
        const rows = stmt.all(limit);
        return rows.map(fromDatabase);
    }
};
SQLiteCodeReviewRepository = __decorate([
    injectable(),
    __metadata("design:paramtypes", [Object])
], SQLiteCodeReviewRepository);
export { SQLiteCodeReviewRepository };
