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';
let CreatePmProjectUseCase = class CreatePmProjectUseCase {
    projectRepo;
    stateRepo;
    constructor(projectRepo, stateRepo) {
        this.projectRepo = projectRepo;
        this.stateRepo = stateRepo;
    }
    async execute(input) {
        const trimmedName = input.name.trim();
        if (!trimmedName) {
            return { ok: false, error: 'Project name is required.' };
        }
        const prefix = input.identifierPrefix.trim().toUpperCase();
        if (!prefix || !/^[A-Z][A-Z0-9]{0,4}$/.test(prefix)) {
            return {
                ok: false,
                error: 'Identifier prefix must be 1-5 uppercase alphanumeric characters starting with a letter.',
            };
        }
        const existing = await this.projectRepo.findByIdentifierPrefix(prefix);
        if (existing) {
            return { ok: false, error: `Identifier prefix "${prefix}" is already in use.` };
        }
        const slug = trimmedName
            .toLowerCase()
            .replace(/[^a-z0-9]+/g, '-')
            .replace(/^-|-$/g, '');
        if (!slug) {
            return { ok: false, error: 'Project name must contain at least one letter or number.' };
        }
        const existingSlug = await this.projectRepo.findBySlug(slug);
        if (existingSlug) {
            return { ok: false, error: `A project with slug "${slug}" already exists.` };
        }
        const now = new Date();
        const project = {
            id: randomUUID(),
            name: trimmedName,
            slug,
            description: input.description,
            identifierPrefix: prefix,
            workItemCounter: 0,
            estimateType: input.estimateType ?? 'Category',
            applicationId: input.applicationId,
            createdAt: now,
            updatedAt: now,
        };
        await this.projectRepo.create(project);
        await this.stateRepo.seedDefaultStates(project.id);
        return { ok: true, project };
    }
};
CreatePmProjectUseCase = __decorate([
    injectable(),
    __param(0, inject('IPmProjectRepository')),
    __param(1, inject('IWorkItemStateRepository')),
    __metadata("design:paramtypes", [Object, Object])
], CreatePmProjectUseCase);
export { CreatePmProjectUseCase };
