'use server';
import { resolve } from '../../lib/server-container.js';
export async function createModule(input) {
    if (!input.name?.trim()) {
        return { error: 'Module name is required' };
    }
    try {
        const useCase = resolve('CreateModuleUseCase');
        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 { module: result.module };
    }
    catch (error) {
        const message = error instanceof Error ? error.message : 'Failed to create module';
        return { error: message };
    }
}
export async function updateModule(moduleId, fields) {
    try {
        const useCase = resolve('UpdateModuleUseCase');
        const result = await useCase.execute(moduleId, fields);
        if (!result.ok)
            return { error: result.error };
        return { module: result.module };
    }
    catch (error) {
        const message = error instanceof Error ? error.message : 'Failed to update module';
        return { error: message };
    }
}
export async function deleteModule(moduleId) {
    try {
        const useCase = resolve('DeleteModuleUseCase');
        const result = await useCase.execute(moduleId);
        if (!result.ok)
            return { error: result.error };
        return {};
    }
    catch (error) {
        const message = error instanceof Error ? error.message : 'Failed to delete module';
        return { error: message };
    }
}
export async function addItemsToModule(moduleId, workItemIds) {
    try {
        const useCase = resolve('AddItemsToModuleUseCase');
        const result = await useCase.execute(moduleId, 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 module';
        return { error: message };
    }
}
export async function removeItemsFromModule(moduleId, workItemIds) {
    try {
        const useCase = resolve('RemoveItemsFromModuleUseCase');
        const result = await useCase.execute(moduleId, workItemIds);
        if (!result.ok)
            return { error: result.error };
        return {};
    }
    catch (error) {
        const message = error instanceof Error ? error.message : 'Failed to remove items from module';
        return { error: message };
    }
}
