/**
 * SQLite Repository Repository Implementation
 *
 * Implements IRepositoryRepository using better-sqlite3.
 */
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 SQLiteRepositoryRepository_1;
import { injectable } from 'tsyringe';
import { toDatabase, fromDatabase, } from '../persistence/sqlite/mappers/repository.mapper.js';
let SQLiteRepositoryRepository = SQLiteRepositoryRepository_1 = class SQLiteRepositoryRepository {
    db;
    constructor(db) {
        this.db = db;
    }
    async create(repository) {
        const row = toDatabase(repository);
        const insertStmt = this.db.prepare(`
      INSERT OR IGNORE INTO repositories (id, name, path, bedrock_enabled, created_at, updated_at)
      VALUES (@id, @name, @path, @bedrock_enabled, @created_at, @updated_at)
    `);
        insertStmt.run(row);
        const selectStmt = this.db.prepare('SELECT * FROM repositories WHERE path = ?');
        const existing = selectStmt.get(row.path);
        return fromDatabase(existing);
    }
    async findById(id) {
        const stmt = this.db.prepare('SELECT * FROM repositories WHERE id = ? AND deleted_at IS NULL');
        const row = stmt.get(id);
        return row ? fromDatabase(row) : null;
    }
    async findByPath(path) {
        const stmt = this.db.prepare("SELECT * FROM repositories WHERE REPLACE(path, '\\', '/') = ? AND deleted_at IS NULL");
        const row = stmt.get(path.replace(/\\/g, '/'));
        return row ? fromDatabase(row) : null;
    }
    async list() {
        const stmt = this.db.prepare('SELECT * FROM repositories WHERE deleted_at IS NULL ORDER BY created_at ASC');
        const rows = stmt.all();
        return rows.map(fromDatabase);
    }
    async remove(id) {
        const stmt = this.db.prepare('DELETE FROM repositories WHERE id = ?');
        stmt.run(id);
    }
    async findByPathIncludingDeleted(path) {
        const stmt = this.db.prepare("SELECT * FROM repositories WHERE REPLACE(path, '\\', '/') = ?");
        const row = stmt.get(path.replace(/\\/g, '/'));
        return row ? fromDatabase(row) : null;
    }
    async findByRemoteUrl(url) {
        const normalized = SQLiteRepositoryRepository_1.normalizeRemoteUrl(url);
        const stmt = this.db.prepare('SELECT * FROM repositories WHERE remote_url = ? AND deleted_at IS NULL');
        const row = stmt.get(normalized);
        return row ? fromDatabase(row) : null;
    }
    async findByUpstreamUrl(url) {
        const normalized = SQLiteRepositoryRepository_1.normalizeRemoteUrl(url);
        const stmt = this.db.prepare('SELECT * FROM repositories WHERE upstream_url = ? AND deleted_at IS NULL');
        const row = stmt.get(normalized);
        return row ? fromDatabase(row) : null;
    }
    async update(id, fields) {
        const now = Date.now();
        const setClauses = ['updated_at = ?'];
        const values = [now];
        if (fields.name !== undefined) {
            setClauses.push('name = ?');
            values.push(fields.name);
        }
        if (fields.path !== undefined) {
            setClauses.push('path = ?');
            values.push(fields.path);
        }
        if (fields.remoteUrl !== undefined) {
            setClauses.push('remote_url = ?');
            values.push(fields.remoteUrl);
        }
        if (fields.isFork !== undefined) {
            setClauses.push('is_fork = ?');
            values.push(fields.isFork ? 1 : 0);
        }
        if (fields.upstreamUrl !== undefined) {
            setClauses.push('upstream_url = ?');
            values.push(fields.upstreamUrl);
        }
        if (fields.bedrockEnabled !== undefined) {
            setClauses.push('bedrock_enabled = ?');
            values.push(fields.bedrockEnabled ? 1 : 0);
        }
        values.push(id);
        const stmt = this.db.prepare(`UPDATE repositories SET ${setClauses.join(', ')} WHERE id = ?`);
        const result = stmt.run(...values);
        if (result.changes === 0) {
            throw new Error(`Repository not found: ${id}`);
        }
        const selectStmt = this.db.prepare('SELECT * FROM repositories WHERE id = ?');
        const row = selectStmt.get(id);
        return fromDatabase(row);
    }
    async softDelete(id) {
        const now = Date.now();
        const stmt = this.db.prepare('UPDATE repositories SET deleted_at = ?, updated_at = ? WHERE id = ?');
        stmt.run(now, now, id);
    }
    async restore(id) {
        const now = Date.now();
        const stmt = this.db.prepare('UPDATE repositories SET deleted_at = NULL, created_at = ?, updated_at = ? WHERE id = ?');
        stmt.run(now, now, id);
    }
    static normalizeRemoteUrl(url) {
        return url.replace(/\.git$/, '').toLowerCase();
    }
};
SQLiteRepositoryRepository = SQLiteRepositoryRepository_1 = __decorate([
    injectable(),
    __metadata("design:paramtypes", [Object])
], SQLiteRepositoryRepository);
export { SQLiteRepositoryRepository };
