'use server';
import { resolve } from '../../lib/server-container.js';
export async function createCycle(input) {
    if (!input.name?.trim()) {
        return { error: 'Cycle name is required' };
    }
    try {
        const useCase = resolve('CreateCycleUseCase');
        const result = await useCase.execute({
            ...input,
            startDate: input.startDate ? new Date(input.startDate) : undefined,
            endDate: input.endDate ? new Date(input.endDate) : undefined,
        });
        if (!result.ok)
            return { error: result.error };
        return { cycle: result.cycle };
    }
    catch (error) {
        const message = error instanceof Error ? error.message : 'Failed to create cycle';
        return { error: message };
    }
}
export async function updateCycle(cycleId, fields) {
    try {
        const useCase = resolve('UpdateCycleUseCase');
        const result = await useCase.execute(cycleId, fields);
        if (!result.ok)
            return { error: result.error };
        return { cycle: result.cycle };
    }
    catch (error) {
        const message = error instanceof Error ? error.message : 'Failed to update cycle';
        return { error: message };
    }
}
export async function deleteCycle(cycleId) {
    try {
        const useCase = resolve('DeleteCycleUseCase');
        const result = await useCase.execute(cycleId);
        if (!result.ok)
            return { error: result.error };
        return {};
    }
    catch (error) {
        const message = error instanceof Error ? error.message : 'Failed to delete cycle';
        return { error: message };
    }
}
export async function addItemsToCycle(cycleId, workItemIds) {
    try {
        const useCase = resolve('AddItemsToCycleUseCase');
        const result = await useCase.execute(cycleId, workItemIds);
        if (!result.ok)
            return { error: result.error };
        return { added: result.added };
    }
    catch (error) {
        const message = error instanceof Error ? error.message : 'Failed to add items to cycle';
        return { error: message };
    }
}
export async function removeItemsFromCycle(cycleId, workItemIds) {
    try {
        const useCase = resolve('RemoveItemsFromCycleUseCase');
        const result = await useCase.execute(cycleId, workItemIds);
        if (!result.ok)
            return { error: result.error };
        return {};
    }
    catch (error) {
        const message = error instanceof Error ? error.message : 'Failed to remove items from cycle';
        return { error: message };
    }
}
export async function transferCycleItems(sourceCycleId, targetCycleId) {
    try {
        const useCase = resolve('TransferCycleItemsUseCase');
        const result = await useCase.execute({ sourceCycleId, targetCycleId });
        if (!result.ok)
            return { error: result.error };
        return { transferred: result.transferred, kept: result.kept };
    }
    catch (error) {
        const message = error instanceof Error ? error.message : 'Failed to transfer cycle items';
        return { error: message };
    }
}
