import { api } from '$lib/services/api';

export interface ProjectMonthPlan {
  project_id: string;
  project_code: string;
  project_name: string;
  project_status: string | null;
  approved_estimate: number;
  months: Record<string, {
    id: string;
    planned_hours: number | null;
    is_blank: boolean;
  } | null>;
  plan_sum: number;
  reconciliation_status: 'OVER' | 'UNDER' | 'MATCH';
}

export interface BulkUpdateRequest {
  year: number;
  items: Array<{
    project_id: string;
    month: string;
    planned_hours: number | null;
  }>;
}

export interface BulkUpdateResponse {
  message: string;
  summary: {
    created: number;
    updated: number;
    cleared: number;
  };
}

class ProjectMonthPlanService {
  // Note: unwrapResponse strips the {data} wrapper, so this returns the array directly
  async getPlans(year: number): Promise<ProjectMonthPlan[]> {
    return api.get<ProjectMonthPlan[]>(`/project-month-plans?year=${year}`);
  }

  async bulkUpdate(request: BulkUpdateRequest): Promise<BulkUpdateResponse> {
    return api.put<BulkUpdateResponse>('/project-month-plans/bulk', request);
  }
}

export const projectMonthPlanService = new ProjectMonthPlanService();
