import { AgentRunStatus } from '../../../../packages/core/src/domain/generated/output.js';
import { deriveNodeState, deriveProgress, deriveLifecycle, } from '../components/common/feature-node/derive-feature-state.js';
import { isProcessAlive } from '../../../../packages/core/src/infrastructure/services/process/is-process-alive.js';
import { computeWorktreePath } from '../../../../packages/core/src/infrastructure/services/ide-launchers/compute-worktree-path.js';
/**
 * Builds a FeatureNodeData object from a Feature entity and optional AgentRun.
 * This is the single-feature version of the logic in `buildGraphNodes`.
 */
export function buildFeatureNodeData(feature, run, options) {
    // Detect crashed agents: DB says running/pending but PID is dead
    const isActive = run?.status === AgentRunStatus.running || run?.status === AgentRunStatus.pending;
    const pidAlive = isActive && run?.pid ? isProcessAlive(run.pid) : undefined;
    return {
        name: feature.name,
        description: feature.description ?? feature.slug,
        featureId: feature.id,
        lifecycle: deriveLifecycle(feature, run),
        repositoryPath: feature.repositoryPath,
        branch: feature.branch,
        worktreePath: feature.worktreePath ?? computeWorktreePath(feature.repositoryPath, feature.branch),
        specPath: feature.specPath,
        state: deriveNodeState(feature, run, pidAlive !== undefined ? { isPidAlive: pidAlive } : undefined),
        progress: deriveProgress(feature),
        summary: feature.description,
        userQuery: feature.userQuery,
        createdAt: feature.createdAt instanceof Date ? feature.createdAt.getTime() : feature.createdAt,
        ...(feature.fast && { fastMode: true }),
        approvalGates: feature.approvalGates,
        push: feature.push,
        openPr: feature.openPr,
        forkAndPr: feature.forkAndPr,
        commitSpecs: feature.commitSpecs,
        enableEvidence: feature.enableEvidence ?? options?.enableEvidence ?? false,
        commitEvidence: feature.commitEvidence ?? options?.commitEvidence ?? false,
        ciWatchEnabled: feature.ciWatchEnabled ?? options?.ciWatchEnabled ?? true,
        ...(options?.hideCiStatus != null && { hideCiStatus: options.hideCiStatus }),
        ...(options?.repositoryName && { repositoryName: options.repositoryName }),
        ...(options?.baseBranch && { baseBranch: options.baseBranch }),
        ...(options?.oneLiner && { oneLiner: options.oneLiner }),
        ...(options?.remoteUrl && { remoteUrl: options.remoteUrl }),
        ...(run?.agentType && { agentType: run.agentType }),
        ...(run?.modelId && { modelId: run.modelId }),
        ...(run?.error && { errorMessage: run.error }),
        ...(feature.agentRunId != null && { hasAgentRun: true }),
        ...(feature.plan != null && { hasPlan: true }),
        ...(feature.injectSkills && { injectSkills: true }),
        ...(feature.injectedSkills?.length && { injectedSkills: feature.injectedSkills }),
        ...(feature.pr && {
            pr: {
                url: feature.pr.url,
                number: feature.pr.number,
                status: feature.pr.status,
                ciStatus: feature.pr.ciStatus,
                commitHash: feature.pr.commitHash,
                mergeable: feature.pr.mergeable,
            },
        }),
    };
}
