/**
 * Create Feature Use Case
 *
 * Creates a new feature entity with a git worktree for isolated development,
 * initializes the spec directory with YAML templates, then spawns the
 * feature agent to begin autonomous SDLC processing.
 *
 * Business Rules:
 * - Metadata generation delegated to MetadataGenerator service
 * - Slug uniqueness delegated to SlugResolver service
 * - Full user input is preserved as-is in spec.yaml
 * - A git worktree is created at ~/.shep/repos/HASH/wt/SLUG/
 * - Spec YAML files are scaffolded at WORKTREE/specs/NNN-SLUG/
 * - Feature agent is spawned with the spec dir path
 * - specPath is persisted on the Feature record
 * - Initial lifecycle is Requirements
 */
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 { randomUUID } from 'node:crypto';
import { SdlcLifecycle, AgentRunStatus } from '../../../../domain/generated/output.js';
import { getSettings } from '../../../../infrastructure/services/settings.service.js';
import { MetadataGenerator } from './metadata-generator.js';
import { SlugResolver } from './slug-resolver.js';
let CreateFeatureUseCase = class CreateFeatureUseCase {
    featureRepo;
    worktreeService;
    agentProcess;
    runRepository;
    specInitializer;
    metadataGenerator;
    slugResolver;
    constructor(featureRepo, worktreeService, agentProcess, runRepository, specInitializer, metadataGenerator, slugResolver) {
        this.featureRepo = featureRepo;
        this.worktreeService = worktreeService;
        this.agentProcess = agentProcess;
        this.runRepository = runRepository;
        this.specInitializer = specInitializer;
        this.metadataGenerator = metadataGenerator;
        this.slugResolver = slugResolver;
    }
    async execute(input) {
        const metadata = await this.metadataGenerator.generateMetadata(input.userInput);
        const originalSlug = metadata.slug;
        // Find a unique slug — the branch may exist from a previous (deleted) feature
        const { slug, branch, warning } = await this.slugResolver.resolveUniqueSlug(originalSlug, input.repositoryPath);
        // Determine next feature number for this repo
        const existingFeatures = await this.featureRepo.list({
            repositoryPath: input.repositoryPath,
        });
        const featureNumber = existingFeatures.length + 1;
        const now = new Date();
        const runId = randomUUID();
        // Create git worktree for isolated development
        const worktreePath = this.worktreeService.getWorktreePath(input.repositoryPath, branch);
        await this.worktreeService.create(input.repositoryPath, branch, worktreePath);
        // Initialize spec directory — full user input goes into spec.yaml as-is
        const { specDir } = await this.specInitializer.initialize(worktreePath, slug, featureNumber, input.userInput);
        const feature = {
            id: randomUUID(),
            name: metadata.name,
            slug,
            description: metadata.description,
            repositoryPath: input.repositoryPath,
            branch,
            lifecycle: SdlcLifecycle.Requirements,
            messages: [],
            relatedArtifacts: [],
            agentRunId: runId,
            specPath: specDir,
            createdAt: now,
            updatedAt: now,
        };
        await this.featureRepo.create(feature);
        // Create agent run record and spawn background worker
        const settings = getSettings();
        const agentRun = {
            id: runId,
            agentType: settings.agent.type,
            agentName: 'feature-agent',
            status: AgentRunStatus.pending,
            prompt: input.userInput,
            threadId: randomUUID(),
            featureId: feature.id,
            repositoryPath: input.repositoryPath,
            ...(input.approvalGates ? { approvalGates: input.approvalGates } : {}),
            createdAt: now.toISOString(),
            updatedAt: now.toISOString(),
        };
        await this.runRepository.create(agentRun);
        this.agentProcess.spawn(feature.id, runId, input.repositoryPath, specDir, worktreePath, {
            ...(input.approvalGates ? { approvalGates: input.approvalGates } : {}),
            threadId: agentRun.threadId,
        });
        return { feature, warning };
    }
};
CreateFeatureUseCase = __decorate([
    injectable(),
    __param(0, inject('IFeatureRepository')),
    __param(1, inject('IWorktreeService')),
    __param(2, inject('IFeatureAgentProcessService')),
    __param(3, inject('IAgentRunRepository')),
    __param(4, inject('ISpecInitializerService')),
    __param(5, inject(MetadataGenerator)),
    __param(6, inject(SlugResolver)),
    __metadata("design:paramtypes", [Object, Object, Object, Object, Object, MetadataGenerator,
        SlugResolver])
], CreateFeatureUseCase);
export { CreateFeatureUseCase };
