/**
 * Delete Feature Use Case
 *
 * Orchestrates feature deletion using a two-phase soft-delete approach:
 * 1. Immediately soft-delete the feature (set deletedAt + lifecycle=Deleting)
 *    so it vanishes from all queries — prevents the "reappear" bug
 * 2. Then perform cleanup (cancel agents, remove worktree/branches)
 *
 * Business Rules:
 * - Throws if the feature does not exist
 * - Cascade soft-deletes all sub-features before deleting the parent
 * - Cancels running/pending agent runs
 * - Gracefully handles worktree removal failures
 */
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 { AgentRunStatus, PrStatus, SdlcLifecycle } from '../../../domain/generated/output.js';
let DeleteFeatureUseCase = class DeleteFeatureUseCase {
    featureRepo;
    worktreeService;
    processService;
    runRepo;
    gitPrService;
    constructor(featureRepo, worktreeService, processService, runRepo, gitPrService) {
        this.featureRepo = featureRepo;
        this.worktreeService = worktreeService;
        this.processService = processService;
        this.runRepo = runRepo;
        this.gitPrService = gitPrService;
    }
    async execute(featureId, options) {
        // 1. Find feature (exact or prefix match)
        const feature = (await this.featureRepo.findById(featureId)) ??
            (await this.featureRepo.findByIdPrefix(featureId));
        if (!feature) {
            throw new Error(`Feature not found: "${featureId}"`);
        }
        const cascadeDelete = options?.cascadeDelete === true;
        // 2. Immediately soft-delete the feature (and children if cascading)
        //    This makes them vanish from all queries right away (no reappear bug)
        if (cascadeDelete) {
            await this.cascadeSoftDelete(feature.id);
        }
        else {
            // Relocate direct children one level up in the hierarchy
            await this.relocateChildren(feature.id, feature.parentId);
        }
        await this.markDeletingAndSoftDelete(feature);
        // 3. Then perform cleanup (best-effort, feature is already hidden)
        if (cascadeDelete) {
            await this.cascadeCleanupChildren(feature.id, options);
        }
        await this.cleanupSingleFeature(feature, options);
        return feature;
    }
    /** Relocate direct children one level up (set their parentId to the deleted feature's parentId). */
    async relocateChildren(parentId, newParentId) {
        const children = await this.featureRepo.findByParentId(parentId);
        for (const child of children) {
            await this.featureRepo.update({
                ...child,
                parentId: newParentId,
                updatedAt: new Date(),
            });
        }
    }
    /** Recursively soft-delete all children (depth-first). */
    async cascadeSoftDelete(parentId) {
        const children = await this.featureRepo.findByParentId(parentId);
        for (const child of children) {
            await this.cascadeSoftDelete(child.id);
            await this.markDeletingAndSoftDelete(child);
        }
    }
    /** Set lifecycle to Deleting and soft-delete the feature. */
    async markDeletingAndSoftDelete(feature) {
        // Update lifecycle to Deleting so it's visible as "deleting" if queried with includeDeleted
        await this.featureRepo.update({
            ...feature,
            lifecycle: SdlcLifecycle.Deleting,
            updatedAt: new Date(),
        });
        // Soft-delete: sets deletedAt, hiding from normal queries
        await this.featureRepo.softDelete(feature.id);
        // Sync the in-memory feature object so subsequent update() calls
        // (e.g. PR status update in cleanup) don't overwrite deleted_at back to null.
        feature.lifecycle = SdlcLifecycle.Deleting;
        feature.deletedAt = new Date();
    }
    /** Recursively cleanup all children (depth-first). */
    async cascadeCleanupChildren(parentId, options) {
        // findByParentId includes all children regardless of soft-delete status
        const children = await this.featureRepo.findByParentId(parentId);
        for (const child of children) {
            await this.cascadeCleanupChildren(child.id, options);
            await this.cleanupSingleFeature(child, options);
        }
    }
    /** Cancel agent runs and clean up worktree/branches for a single feature. */
    async cleanupSingleFeature(feature, options) {
        // Cancel running/pending agent run if present
        if (feature.agentRunId) {
            const run = await this.runRepo.findById(feature.agentRunId);
            if (run && (run.status === AgentRunStatus.running || run.status === AgentRunStatus.pending)) {
                if (run.pid && this.processService.isAlive(run.pid)) {
                    try {
                        process.kill(run.pid);
                    }
                    catch {
                        // Process may have already exited
                    }
                }
                await this.runRepo.updateStatus(run.id, AgentRunStatus.cancelled);
            }
        }
        // Cleanup worktree and branches directly using the feature data we already
        // have (CleanupFeatureWorktreeUseCase.execute() would fail because
        // findById() excludes soft-deleted features).
        const worktreePath = feature.worktreePath ??
            this.worktreeService.getWorktreePath(feature.repositoryPath, feature.branch);
        try {
            await this.worktreeService.remove(feature.repositoryPath, worktreePath, true);
        }
        catch {
            // Worktree might already be removed - that's fine
        }
        const cleanup = options?.cleanup !== false;
        if (cleanup) {
            const shouldDeleteRemote = options?.closePr !== false;
            // Delete local branch, and also remote if closePr is enabled.
            // deleteBranch(cwd, branch, true) deletes local then remote in one call,
            // so we must NOT delete local separately when also deleting remote.
            if (shouldDeleteRemote) {
                let remoteDeleted = false;
                try {
                    const remoteExists = await this.worktreeService.remoteBranchExists(feature.repositoryPath, feature.branch);
                    if (remoteExists) {
                        // Delete local + remote in one call (deleteBranch does local first, then remote)
                        await this.gitPrService.deleteBranch(feature.repositoryPath, feature.branch, true);
                        remoteDeleted = true;
                    }
                    else {
                        // No remote branch — just delete local
                        await this.gitPrService.deleteBranch(feature.repositoryPath, feature.branch);
                    }
                }
                catch {
                    // Branch cleanup is best-effort
                }
                // Update pr.status to Closed after remote branch deletion (GitHub auto-closes the PR)
                if (remoteDeleted && feature.pr?.status === PrStatus.Open) {
                    try {
                        await this.featureRepo.update({
                            ...feature,
                            pr: { ...feature.pr, status: PrStatus.Closed },
                            updatedAt: new Date(),
                        });
                    }
                    catch {
                        // PR status update is best-effort
                    }
                }
            }
            else {
                // closePr is false — only delete local branch, preserve remote (and the PR)
                try {
                    await this.gitPrService.deleteBranch(feature.repositoryPath, feature.branch);
                }
                catch {
                    // Branch might not exist
                }
            }
        }
    }
};
DeleteFeatureUseCase = __decorate([
    injectable(),
    __param(0, inject('IFeatureRepository')),
    __param(1, inject('IWorktreeService')),
    __param(2, inject('IFeatureAgentProcessService')),
    __param(3, inject('IAgentRunRepository')),
    __param(4, inject('IGitPrService')),
    __metadata("design:paramtypes", [Object, Object, Object, Object, Object])
], DeleteFeatureUseCase);
export { DeleteFeatureUseCase };
