import { LoggerService } from '../../services/core/logger.service.js';
const logger = LoggerService.getInstance().createComponentLogger('AssignmentsController');
export async function getAssignments(req, res) {
    try {
        const projects = await this.storageService.getProjects();
        const teams = await this.storageService.getTeams();
        const assignments = [];
        for (const project of projects) {
            for (const teamId of Object.values(project.teams).flat()) {
                const team = teams.find(t => t.id === teamId);
                if (team) {
                    assignments.push({
                        id: `${project.id}-${teamId}`,
                        title: `${project.name} - ${team.name}`,
                        description: 'No description available',
                        status: project.status === 'active' ? 'in-progress' : 'todo',
                        assignedTo: team.members[0]?.name || 'Unassigned',
                        priority: 'medium',
                        teamId: team.id,
                        teamName: team.name,
                        createdAt: project.createdAt,
                        dueDate: undefined,
                        tags: [team.members[0]?.role || 'general']
                    });
                }
            }
        }
        res.json(assignments);
    }
    catch (error) {
        logger.error('Error fetching assignments', { error: error instanceof Error ? error.message : String(error) });
        res.status(500).json({ success: false, error: 'Failed to fetch assignments' });
    }
}
export async function updateAssignment(req, res) {
    try {
        const { id } = req.params;
        const { status } = req.body;
        if (status && ['todo', 'in-progress', 'review', 'done'].includes(status)) {
            const projects = await this.storageService.getProjects();
            const assignmentId = String(id ?? '');
            const project = projects.find(p => assignmentId === p.id || assignmentId.startsWith(`${p.id}-`));
            if (project) {
                let nextProjectStatus = null;
                if (status === 'in-progress')
                    nextProjectStatus = 'active';
                else if (status === 'done')
                    nextProjectStatus = 'completed';
                if (nextProjectStatus && project.status !== nextProjectStatus) {
                    project.status = nextProjectStatus;
                    await this.storageService.saveProject(project);
                }
            }
        }
        res.json({ success: true, message: 'Assignment updated successfully' });
    }
    catch (error) {
        logger.error('Error updating assignment', { error: error instanceof Error ? error.message : String(error) });
        res.status(500).json({ success: false, error: 'Failed to update assignment' });
    }
}
//# sourceMappingURL=assignments.controller.js.map