"use server";

// import { serverFetchJson } from "@/app/utils/fetchUtil";
// import { BASE_API_URL } from "@/config/api";
import { serverFetchJson } from "../../utils/fetchUtil";
import { BASE_API_URL } from "../../../config/api";
import { Task, TaskGroup } from "../tasks";

export interface CreateProjectInputs {
  // Project details
  projectCode: string;
  projectName: string;
  projectCategory: string;
  projectDescription: string;

  // Client details
  clientCode: string;
  clientName: string;
  clientContactName: string;
  clientPhone: string;
  clientEmail: string;
  clientSubsidiary: string;

  // Tasks
  tasks: {
    [taskId: Task["id"]]: {
      manhourAllocation: ManhourAllocation;
    };
  };

  // Staff
  allocatedStaffIds: number[];

  // Milestones
  milestones: {
    [taskGroupId: TaskGroup["id"]]: {
      startDate: string;
      endDate: string;
      payments: PaymentInputs[];
    };
  };
}

export interface ManhourAllocation {
  [gradeId: number]: number;
}

export interface PaymentInputs {
  id: number;
  description: string;
  date: string;
  amount: number;
}

export const saveProject = async (data: CreateProjectInputs) => {
  return serverFetchJson(`${BASE_API_URL}/projects/new`, {
    method: "POST",
    body: JSON.stringify(data),
    headers: { "Content-Type": "application/json" },
  });
};
