"use server";

import { serverFetchJson, serverFetchString } from "@/app/utils/fetchUtil";
import { ProjectResult } from "../projects";
import { Task, TaskGroup } from "../tasks";
import { BASE_API_URL } from "@/config/api";
import { revalidatePath, revalidateTag } from "next/cache";

export interface TimeEntry {
  id: number;
  projectId?: ProjectResult["id"];
  taskGroupId?: TaskGroup["id"];
  taskId?: Task["id"];
  inputHours?: number;
  otHours?: number;
  remark?: string;
}

export interface RecordTimesheetInput {
  [date: string]: TimeEntry[];
}

export interface LeaveEntry {
  id: number;
  inputHours: number;
  leaveTypeId: number;
  remark?: string;
}

export interface RecordLeaveInput {
  [date: string]: LeaveEntry[];
}

export type TimeLeaveEntry =
  | (TimeEntry & { type: "timeEntry" })
  | (LeaveEntry & { type: "leaveEntry" });

export interface RecordTimeLeaveInput {
  [date: string]: TimeLeaveEntry[];
}

export const saveTimesheet = async (data: RecordTimesheetInput) => {
  const savedRecords = await serverFetchJson<RecordTimesheetInput>(
    `${BASE_API_URL}/timesheets/save`,
    {
      method: "POST",
      body: JSON.stringify(data),
      headers: { "Content-Type": "application/json" },
    },
  );

  revalidateTag(`timesheets`);

  return savedRecords;
};

export const saveLeave = async (data: RecordLeaveInput) => {
  const savedRecords = await serverFetchJson<RecordLeaveInput>(
    `${BASE_API_URL}/timesheets/saveLeave`,
    {
      method: "POST",
      body: JSON.stringify(data),
      headers: { "Content-Type": "application/json" },
    },
  );

  revalidateTag(`leaves`);

  return savedRecords;
};

export const saveTimeLeave = async (data: RecordTimeLeaveInput) => {
  const savedRecords = await serverFetchJson<RecordTimeLeaveInput>(
    `${BASE_API_URL}/timesheets/saveTimeLeave`,
    {
      method: "POST",
      body: JSON.stringify(data),
      headers: { "Content-Type": "application/json" },
    },
  );

  revalidateTag(`timesheets`);
  revalidateTag(`leaves`);

  return savedRecords;
};

export const saveMemberEntry = async (data: {
  staffId: number;
  entry: TimeEntry;
  recordDate?: string;
}) => {
  return serverFetchJson<RecordTimesheetInput>(
    `${BASE_API_URL}/timesheets/saveMemberEntry`,
    {
      method: "POST",
      body: JSON.stringify(data),
      headers: { "Content-Type": "application/json" },
    },
  );
};

export const saveMemberLeave = async (data: {
  staffId: number;
  entry: LeaveEntry;
  recordDate?: string;
}) => {
  return serverFetchJson<RecordLeaveInput>(
    `${BASE_API_URL}/timesheets/saveMemberLeave`,
    {
      method: "POST",
      body: JSON.stringify(data),
      headers: { "Content-Type": "application/json" },
    },
  );
};

export const deleteMemberEntry = async (data: {
  staffId: number;
  entryId: number;
}) => {
  return serverFetchJson<RecordTimesheetInput>(
    `${BASE_API_URL}/timesheets/deleteMemberEntry`,
    {
      method: "POST",
      body: JSON.stringify(data),
      headers: { "Content-Type": "application/json" },
    },
  );
};

export const deleteMemberLeave = async (data: {
  staffId: number;
  entryId: number;
}) => {
  return serverFetchJson<RecordLeaveInput>(
    `${BASE_API_URL}/timesheets/deleteMemberLeave`,
    {
      method: "POST",
      body: JSON.stringify(data),
      headers: { "Content-Type": "application/json" },
    },
  );
};

export const revalidateCacheAfterAmendment = () => {
  revalidatePath("/(main)/home");
};

export const importTimesheets = async (data: FormData) => {
  const importTimesheets = await serverFetchString<string>(
    `${BASE_API_URL}/timesheets/import`,
    {
      method: "POST",
      body: data,
    },
  );

  return importTimesheets;
};

export const importOSTimesheets = async (data: FormData) => {
  const importTimesheets = await serverFetchString<string>(
    `${BASE_API_URL}/timesheets/import-OS`,
    {
      method: "POST",
      body: data,
    },
  );

  return importTimesheets;
};

export const importLeaves = async (data: FormData) => {
  const importLeaves = await serverFetchString<string>(
    `${BASE_API_URL}/timesheets/import-leave`,
    {
      method: "POST",
      body: data,
    },
  );

  return importLeaves;
};

export const rearrangeTimesheets = async () => {
  const importLeaves = await serverFetchString<string>(
    `${BASE_API_URL}/timesheets/rearrange`,
    {
      method: "POST",
      // body: data,
    },
  );

  return importLeaves;
};