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 GetProjectBreakdownUseCase = class GetProjectBreakdownUseCase {
    projectRepo;
    workItemRepo;
    stateRepo;
    constructor(projectRepo, workItemRepo, stateRepo) {
        this.projectRepo = projectRepo;
        this.workItemRepo = workItemRepo;
        this.stateRepo = stateRepo;
    }
    async execute(projectId) {
        const project = await this.projectRepo.findById(projectId);
        if (!project) {
            return { ok: false, error: `Project not found: "${projectId}"` };
        }
        const workItems = await this.workItemRepo.listByProject(projectId);
        const states = await this.stateRepo.listByProject(projectId);
        const stateMap = new Map(states.map((s) => [s.id, s]));
        const stateCounts = new Map();
        const priorityCounts = new Map();
        const stateGroupCounts = new Map();
        for (const item of workItems) {
            stateCounts.set(item.stateId, (stateCounts.get(item.stateId) ?? 0) + 1);
            priorityCounts.set(item.priority, (priorityCounts.get(item.priority) ?? 0) + 1);
            const state = stateMap.get(item.stateId);
            if (state) {
                stateGroupCounts.set(state.stateGroup, (stateGroupCounts.get(state.stateGroup) ?? 0) + 1);
            }
        }
        const byState = states.map((s) => ({
            stateId: s.id,
            stateName: s.name,
            stateGroup: s.stateGroup,
            stateColor: s.color,
            count: stateCounts.get(s.id) ?? 0,
        }));
        const priorityOrder = ['Urgent', 'High', 'Medium', 'Low', 'None'];
        const byPriority = priorityOrder.map((p) => ({
            priority: p,
            count: priorityCounts.get(p) ?? 0,
        }));
        const groupOrder = ['Backlog', 'Unstarted', 'Started', 'Completed', 'Cancelled'];
        const byStateGroup = groupOrder.map((g) => ({
            group: g,
            count: stateGroupCounts.get(g) ?? 0,
        }));
        return {
            ok: true,
            data: {
                projectId,
                projectName: project.name,
                totalItems: workItems.length,
                byState,
                byPriority,
                byStateGroup,
            },
        };
    }
};
GetProjectBreakdownUseCase = __decorate([
    injectable(),
    __param(0, inject('IPmProjectRepository')),
    __param(1, inject('IWorkItemRepository')),
    __param(2, inject('IWorkItemStateRepository')),
    __metadata("design:paramtypes", [Object, Object, Object])
], GetProjectBreakdownUseCase);
export { GetProjectBreakdownUseCase };
