/**
 * SdlcBoardTracker — Infrastructure adapter for the ISdlcBoardTracker port.
 *
 * Bridges the feature-agent to the SDLC Board by translating high-level,
 * key-addressed calls (`seedTasks`, `setTaskStatus`, `setSubTaskStatus`) into
 * concrete repository operations (`upsertByKey`, `updateStatus`).
 *
 * Design notes:
 * - IDs are generated with `randomUUID()` from `node:crypto`, consistent with
 *   every other infrastructure adapter in this codebase.
 * - `seedTasks` is idempotent: the repository's `upsertByKey` guarantees no
 *   duplicate rows when called with the same (featureId, taskKey) or
 *   (taskId, subTaskKey) combination.
 * - `setTaskStatus` / `setSubTaskStatus` resolve entities by their stable
 *   string keys; if a key is not found the call resolves without error.
 */
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 { randomUUID } from 'node:crypto';
import { inject, injectable } from 'tsyringe';
import { TaskState } from '../../../domain/generated/output.js';
let SdlcBoardTracker = class SdlcBoardTracker {
    taskRepo;
    subTaskRepo;
    constructor(taskRepo, subTaskRepo) {
        this.taskRepo = taskRepo;
        this.subTaskRepo = subTaskRepo;
    }
    /**
     * Idempotent upsert of the entire task plan for a feature.
     *
     * Generates a fresh UUID for each task and sub-task on every call — the
     * repository's upsert-by-key logic ignores the id when a row already
     * exists, so duplicate UUIDs are never written.
     */
    async seedTasks(featureId, tasks) {
        for (const task of tasks) {
            const taskId = randomUUID();
            await this.taskRepo.upsertByKey(taskId, featureId, task.taskKey, {
                title: task.title,
                description: task.description,
                status: task.status ?? TaskState.Todo,
                sortOrder: task.sortOrder,
                branch: task.branch,
                dependsOnKeys: task.dependsOnKeys,
                agentRunId: task.agentRunId,
            });
            for (const subTask of task.subTasks) {
                const subTaskId = randomUUID();
                await this.subTaskRepo.upsertByKey(subTaskId, taskId, subTask.subTaskKey, {
                    featureId,
                    name: subTask.name,
                    description: subTask.description,
                    status: subTask.status ?? TaskState.Todo,
                    sortOrder: subTask.sortOrder,
                });
            }
        }
    }
    /**
     * Transition a task's workflow state.
     *
     * Resolves the task by its stable `taskKey` within the given `featureId`.
     * No-ops gracefully when the key is not found.
     */
    async setTaskStatus(featureId, taskKey, status) {
        const tasks = await this.taskRepo.listByFeature(featureId);
        const task = tasks.find((t) => t.taskKey === taskKey);
        if (!task) {
            return;
        }
        await this.taskRepo.updateStatus(task.id, status);
    }
    /**
     * Transition a sub-task's workflow state.
     *
     * First resolves the parent task by `taskKey` within `featureId`, then
     * resolves the sub-task by `subTaskKey` within that task. No-ops
     * gracefully when either key is not found.
     */
    async setSubTaskStatus(featureId, taskKey, subTaskKey, status) {
        const tasks = await this.taskRepo.listByFeature(featureId);
        const task = tasks.find((t) => t.taskKey === taskKey);
        if (!task) {
            return;
        }
        const subTasks = await this.subTaskRepo.listByTask(task.id);
        const subTask = subTasks.find((st) => st.subTaskKey === subTaskKey);
        if (!subTask) {
            return;
        }
        await this.subTaskRepo.updateStatus(subTask.id, status);
    }
};
SdlcBoardTracker = __decorate([
    injectable(),
    __param(0, inject('ISdlcTaskRepository')),
    __param(1, inject('ISdlcSubTaskRepository')),
    __metadata("design:paramtypes", [Object, Object])
], SdlcBoardTracker);
export { SdlcBoardTracker };
