/**
 * Auto-Archive Completed Use Case
 *
 * Scans for features in the Maintain (completed) lifecycle whose updatedAt
 * timestamp is older than the configured delay, and archives them automatically.
 *
 * Business Rules:
 * - Only features in Maintain lifecycle are candidates
 * - The delay is read from settings (workflow.autoArchiveDelayMinutes)
 * - A delay of 0 disables auto-archiving
 * - Uses the existing ArchiveFeatureUseCase for each archival to maintain
 *   consistent lifecycle transitions (previousLifecycle preservation, etc.)
 */
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';
import { SdlcLifecycle } from '../../../domain/generated/output.js';
import { ArchiveFeatureUseCase } from './archive-feature.use-case.js';
const MS_PER_MINUTE = 60_000;
let AutoArchiveCompletedUseCase = class AutoArchiveCompletedUseCase {
    featureRepo;
    archiveFeature;
    constructor(featureRepo, archiveFeature) {
        this.featureRepo = featureRepo;
        this.archiveFeature = archiveFeature;
    }
    /**
     * Archive all features in Maintain state that have been completed longer
     * than the given delay.
     *
     * @param delayMinutes - Minutes after completion before archiving. 0 = disabled.
     * @returns Array of features that were archived.
     */
    async execute(delayMinutes) {
        if (delayMinutes <= 0)
            return [];
        const features = await this.featureRepo.list({
            lifecycle: SdlcLifecycle.Maintain,
        });
        const now = Date.now();
        const thresholdMs = delayMinutes * MS_PER_MINUTE;
        const archived = [];
        for (const feature of features) {
            const updatedMs = feature.updatedAt instanceof Date
                ? feature.updatedAt.getTime()
                : new Date(feature.updatedAt).getTime();
            if (now - updatedMs >= thresholdMs) {
                try {
                    const result = await this.archiveFeature.execute(feature.id);
                    archived.push(result);
                }
                catch {
                    // Feature may have been archived/deleted by another process — skip
                }
            }
        }
        return archived;
    }
};
AutoArchiveCompletedUseCase = __decorate([
    injectable(),
    __param(0, inject('IFeatureRepository')),
    __metadata("design:paramtypes", [Object, ArchiveFeatureUseCase])
], AutoArchiveCompletedUseCase);
export { AutoArchiveCompletedUseCase };
