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 GetCycleBurndownUseCase = class GetCycleBurndownUseCase {
    cycleRepo;
    workItemRepo;
    stateRepo;
    activityRepo;
    constructor(cycleRepo, workItemRepo, stateRepo, activityRepo) {
        this.cycleRepo = cycleRepo;
        this.workItemRepo = workItemRepo;
        this.stateRepo = stateRepo;
        this.activityRepo = activityRepo;
    }
    async execute(cycleId) {
        const cycle = await this.cycleRepo.findById(cycleId);
        if (!cycle) {
            return { ok: false, error: `Cycle not found: "${cycleId}"` };
        }
        if (!cycle.startDate || !cycle.endDate) {
            return { ok: false, error: 'Cycle must have start and end dates for burndown chart.' };
        }
        const workItemIds = await this.cycleRepo.getWorkItemIds(cycleId);
        const totalItems = workItemIds.length;
        const states = await this.stateRepo.listByProject(cycle.projectId);
        const completedStateIds = new Set(states
            .filter((s) => s.stateGroup === 'Completed' || s.stateGroup === 'Cancelled')
            .map((s) => s.id));
        let completedItems = 0;
        for (const itemId of workItemIds) {
            const item = await this.workItemRepo.findById(itemId);
            if (item && completedStateIds.has(item.stateId)) {
                completedItems++;
            }
        }
        const startDate = new Date(cycle.startDate);
        const endDate = new Date(cycle.endDate);
        const today = new Date();
        const totalDays = Math.max(1, Math.ceil((endDate.getTime() - startDate.getTime()) / (1000 * 60 * 60 * 24)));
        const dataPoints = [];
        const idealDecrement = totalItems / totalDays;
        for (let day = 0; day <= totalDays; day++) {
            const currentDate = new Date(startDate);
            currentDate.setDate(currentDate.getDate() + day);
            const dateStr = currentDate.toISOString().split('T')[0];
            const idealRemaining = Math.max(0, totalItems - idealDecrement * day);
            let actualRemaining;
            if (currentDate > today) {
                actualRemaining = totalItems - completedItems;
            }
            else {
                actualRemaining = totalItems - completedItems;
            }
            dataPoints.push({
                date: dateStr,
                ideal: Math.round(idealRemaining * 10) / 10,
                actual: actualRemaining,
            });
        }
        return {
            ok: true,
            data: {
                cycleId,
                cycleName: cycle.name,
                startDate: startDate.toISOString().split('T')[0],
                endDate: endDate.toISOString().split('T')[0],
                totalItems,
                completedItems,
                dataPoints,
            },
        };
    }
};
GetCycleBurndownUseCase = __decorate([
    injectable(),
    __param(0, inject('ICycleRepository')),
    __param(1, inject('IWorkItemRepository')),
    __param(2, inject('IWorkItemStateRepository')),
    __param(3, inject('IActivityLogRepository')),
    __metadata("design:paramtypes", [Object, Object, Object, Object])
], GetCycleBurndownUseCase);
export { GetCycleBurndownUseCase };
