'use server';
import { revalidatePath } from 'next/cache';
import { resolve } from '../../lib/server-container.js';
import { updateSettings as updateSettingsSingleton } from '../../../../../packages/core/src/infrastructure/services/settings.service.js';
export async function addInjectedSkill(skill) {
    try {
        const loadUseCase = resolve('LoadSettingsUseCase');
        const current = await loadUseCase.execute();
        const existingSkills = current.workflow.skillInjection?.skills ?? [];
        if (existingSkills.some((s) => s.name === skill.name)) {
            return { success: false, error: `Skill "${skill.name}" is already configured` };
        }
        const updated = {
            ...current,
            workflow: {
                ...current.workflow,
                skillInjection: {
                    enabled: current.workflow.skillInjection?.enabled ?? true,
                    skills: [...existingSkills, skill],
                },
            },
            updatedAt: new Date(),
        };
        const updateUseCase = resolve('UpdateSettingsUseCase');
        await updateUseCase.execute(updated);
        updateSettingsSingleton(updated);
        revalidatePath('/skills');
        return { success: true };
    }
    catch (error) {
        const message = error instanceof Error ? error.message : 'Failed to add skill';
        return { success: false, error: message };
    }
}
