import { axiosClient } from "@/lib/axios";
import { ApiResponse } from "@/types/api.types";
import {
  PaginationMeta,
  PaginationParams,
  buildQueryParams,
} from "@/lib/pagination";
import {
  AcceptInvitationPayload,
  Employee,
  EmployeeRole,
  InviteEmployeePayload,
} from "../types/employee.type";

export const employeeService = {
  inviteEmployee: async (
    payload: InviteEmployeePayload
  ): Promise<ApiResponse<{ message: string; email: string }>> => {
    const res = await axiosClient.post(`/employees/invite`, payload);
    return res.data;
  },

  acceptInvitation: async (
    payload: AcceptInvitationPayload
  ): Promise<ApiResponse<{ message: string }>> => {
    const res = await axiosClient.post(`/employees/accept-invitation`, payload);
    return res.data;
  },

  getEmployees: async (
    params: PaginationParams
  ): Promise<ApiResponse<Employee[]> & { meta: PaginationMeta }> => {
    const res = await axiosClient.get(
      `/employees/get-all-employees?${buildQueryParams(params)}`
    );
    return res.data;
  },

  getEmployee: async (id: string): Promise<ApiResponse<Employee>> => {
    const res = await axiosClient.get<ApiResponse<Employee>>(
      `/employees/get-employee/${id}`
    );
    return res.data;
  },

  updateEmployee: async (
    id: string,
    payload: { name: string; role: EmployeeRole; departmentId?: string; phone?: string; gender?: string; status?: string }
  ): Promise<ApiResponse<Employee>> => {
    const res = await axiosClient.patch(`/employees/update-employee/${id}`, payload);
    return res.data;
  },

  deleteEmployee: async (id: string): Promise<ApiResponse<null>> => {
    const res = await axiosClient.delete(`/employees/delete-employee/${id}`);
    return res.data;
  },
};
