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/pm-project-member.mapper.js';
let SQLitePmProjectMemberRepository = class SQLitePmProjectMemberRepository {
    db;
    constructor(db) {
        this.db = db;
    }
    async create(member) {
        const row = toDatabase(member);
        const stmt = this.db.prepare(`
      INSERT INTO pm_project_members (
        id, project_id, user_id, role,
        created_at, updated_at, deleted_at
      ) VALUES (
        @id, @project_id, @user_id, @role,
        @created_at, @updated_at, @deleted_at
      )
    `);
        stmt.run(row);
    }
    async findByProjectAndUser(projectId, userId) {
        const stmt = this.db.prepare('SELECT * FROM pm_project_members WHERE project_id = ? AND user_id = ? AND deleted_at IS NULL');
        const row = stmt.get(projectId, userId);
        return row ? fromDatabase(row) : null;
    }
    async listByProject(projectId) {
        const stmt = this.db.prepare('SELECT * FROM pm_project_members WHERE project_id = ? AND deleted_at IS NULL ORDER BY created_at ASC');
        const rows = stmt.all(projectId);
        return rows.map(fromDatabase);
    }
    async listByUser(userId) {
        const stmt = this.db.prepare('SELECT * FROM pm_project_members WHERE user_id = ? AND deleted_at IS NULL ORDER BY created_at ASC');
        const rows = stmt.all(userId);
        return rows.map(fromDatabase);
    }
    async updateRole(id, role) {
        const now = Date.now();
        const stmt = this.db.prepare('UPDATE pm_project_members SET role = ?, updated_at = ? WHERE id = ? AND deleted_at IS NULL');
        stmt.run(role, now, id);
    }
    async softDelete(id) {
        const now = Date.now();
        const stmt = this.db.prepare('UPDATE pm_project_members SET deleted_at = ? WHERE id = ?');
        stmt.run(now, id);
    }
    async countAdmins(projectId) {
        const stmt = this.db.prepare("SELECT COUNT(*) as count FROM pm_project_members WHERE project_id = ? AND role = 'Admin' AND deleted_at IS NULL");
        const result = stmt.get(projectId);
        return result.count;
    }
};
SQLitePmProjectMemberRepository = __decorate([
    injectable(),
    __metadata("design:paramtypes", [Object])
], SQLitePmProjectMemberRepository);
export { SQLitePmProjectMemberRepository };
