'use server';
import { resolve } from '../../lib/server-container.js';
import { buildFeatureNodeData } from '../build-feature-node-data.js';
import { getSettings } from '../../../../../packages/core/src/infrastructure/services/settings.service.js';
/**
 * Fetches full FeatureNodeData for a given feature ID.
 * Used by the drawer for targeted data syncing without triggering
 * a full router.refresh() / server component re-render.
 *
 * CI status and mergeable status are read from the DB (already updated
 * by PrSyncWatcherService) instead of making duplicate GitHub API calls.
 */
export async function getFeatureDrawerData(featureId) {
    try {
        const featureRepo = resolve('IFeatureRepository');
        const agentRunRepo = resolve('IAgentRunRepository');
        const repoRepo = resolve('IRepositoryRepository');
        const gitPrService = resolve('IGitPrService');
        const getArtifact = resolve('GetFeatureArtifactUseCase');
        const feature = await featureRepo.findById(featureId);
        if (!feature)
            return null;
        const run = feature.agentRunId ? await agentRunRepo.findById(feature.agentRunId) : null;
        // CI status and mergeable status are read from the feature record (updated by
        // PrSyncWatcherService every 30s) — no duplicate GitHub API calls needed.
        // Only getDefaultBranch and getRemoteUrl are kept as they are local git operations.
        const [repo, baseBranch, artifact, remoteUrl] = await Promise.all([
            repoRepo.findByPath(feature.repositoryPath).catch(() => null),
            gitPrService.getDefaultBranch(feature.repositoryPath).catch(() => 'main'),
            getArtifact.execute(featureId).catch(() => null),
            gitPrService.getRemoteUrl(feature.repositoryPath).catch(() => null),
        ]);
        const { workflow } = getSettings();
        return buildFeatureNodeData(feature, run, {
            repositoryName: repo?.name,
            baseBranch,
            oneLiner: artifact?.oneLiner,
            remoteUrl: remoteUrl ?? undefined,
            enableEvidence: workflow.enableEvidence,
            commitEvidence: workflow.commitEvidence,
            hideCiStatus: workflow.hideCiStatus,
        });
    }
    catch {
        return null;
    }
}
