// File: src/resources/assignment.js

/**
 * لود تخصیص‌های Task ← Resource
 * @param {Object} state
 * @param {Object} projectData
 */
export function loadAssignments(state, projectData) {
    if (!Array.isArray(projectData.assignments)) {
        return;
    }

    state.assignments.byId = {};
    state.assignments.byTaskId = {};
    state.assignments.byResourceId = {};

    projectData.assignments.forEach(a => {
        const id = a.id;
        if (!id) return;

        const assignment = {
            id,
            taskId: a.task_id ?? null,
            resourceId: a.resource_id ?? null,
            units: a.units ?? 1,               // درصد یا FTE
            costRate: a.cost_rate ?? null,     // نرخ مخصوص این تخصیص
            role: a.role ?? null
        };

        state.assignments.byId[id] = assignment;

        if (assignment.taskId != null) {
            if (!state.assignments.byTaskId[assignment.taskId]) {
                state.assignments.byTaskId[assignment.taskId] = [];
            }
            state.assignments.byTaskId[assignment.taskId].push(id);
        }

        if (assignment.resourceId != null) {
            if (!state.assignments.byResourceId[assignment.resourceId]) {
                state.assignments.byResourceId[assignment.resourceId] = [];
            }
            state.assignments.byResourceId[assignment.resourceId].push(id);
        }
    });
}
