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 __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
import { injectable, inject } from 'tsyringe';
let UpdateProjectMemberRoleUseCase = class UpdateProjectMemberRoleUseCase {
    memberRepo;
    constructor(memberRepo) {
        this.memberRepo = memberRepo;
    }
    async execute(input) {
        // Check actor has Admin role
        const actorMember = await this.memberRepo.findByProjectAndUser(input.projectId, input.actorId);
        if (!actorMember || actorMember.role !== 'Admin') {
            return { ok: false, error: 'Only project admins can change member roles.' };
        }
        const targetMember = await this.memberRepo.findByProjectAndUser(input.projectId, input.userId);
        if (!targetMember) {
            return { ok: false, error: 'User is not a member of this project.' };
        }
        // Prevent demoting the last admin
        if (targetMember.role === 'Admin' && input.newRole !== 'Admin') {
            const adminCount = await this.memberRepo.countAdmins(input.projectId);
            if (adminCount <= 1) {
                return { ok: false, error: 'Cannot demote the last admin of a project.' };
            }
        }
        await this.memberRepo.updateRole(targetMember.id, input.newRole);
        return { ok: true };
    }
};
UpdateProjectMemberRoleUseCase = __decorate([
    injectable(),
    __param(0, inject('IPmProjectMemberRepository')),
    __metadata("design:paramtypes", [Object])
], UpdateProjectMemberRoleUseCase);
export { UpdateProjectMemberRoleUseCase };
