import {
  APIUrlGenerator,
  httpDeleteRequest,
  httpGetRequest,
  httpPostRequest,
  httpPutRequest,
} from "@/utils/baseHttpService";
import { API_ROUTES } from "@/constant/routes";
import {
  TicketAssignRequest,
  TicketDepartmentPayload,
  TicketDepartmentResponse,
  TicketDepartmentsResponse,
  TicketDetail,
  TicketDetailResponse,
  TicketFilters,
  TicketListResponse,
  TicketReplyRequest,
  TicketStatusPayload,
  TicketStatusResponse,
  TicketStatusesResponse,
  TicketStatusUpdateRequest,
  TicketSubjectPayload,
  TicketSubjectResponse,
  TicketSubjectsResponse,
} from "./_models";

export const getTickets = async (filters?: TicketFilters) => {
  const queryParams: Record<string, string | number | null> = {};
  const limitValue = filters?.limit || 20;
  queryParams.limit = limitValue;
  if (filters?.offset !== undefined && filters.offset !== null) {
    queryParams.offset = filters.offset;
  } else if (filters?.page) {
    queryParams.offset = (filters.page - 1) * limitValue;
  }
  if (filters?.status_id) queryParams.status_id = filters.status_id;
  if (filters?.department_id) queryParams.department_id = filters.department_id;
  if (filters?.user_id) queryParams.user_id = filters.user_id;
  if (filters?.assigned_to) queryParams.assigned_to = filters.assigned_to;
  if (filters?.search) queryParams.search = filters.search;
  const response = await httpGetRequest<TicketListResponse>(
    APIUrlGenerator(API_ROUTES.GET_TICKETS, queryParams)
  );
  return response.data;
};

export const getTicket = async (id: string) => {
  const response = await httpGetRequest<TicketDetailResponse>(
    APIUrlGenerator(API_ROUTES.GET_TICKET(id))
  );
  return response.data.ticket as TicketDetail;
};

export const replyToTicket = async (
  ticketId: string,
  payload: TicketReplyRequest
) => {
  const response = await httpPostRequest(
    APIUrlGenerator(API_ROUTES.CREATE_TICKET_REPLY(ticketId)),
    payload
  );
  return response.data;
};

export const updateTicketStatus = async (
  ticketId: string,
  payload: TicketStatusUpdateRequest
) => {
  const response = await httpPutRequest(
    APIUrlGenerator(API_ROUTES.UPDATE_TICKET_STATUS(ticketId)),
    payload
  );
  return response.data;
};

export const assignTicket = async (
  ticketId: string,
  payload: TicketAssignRequest
) => {
  const response = await httpPutRequest(
    APIUrlGenerator(API_ROUTES.ASSIGN_TICKET(ticketId)),
    payload
  );
  return response.data;
};

export const getTicketDepartments = async (params?: {
  activeOnly?: boolean;
}) => {
  const queryParams: Record<string, string | number | null> = {};
  if (typeof params?.activeOnly === "boolean") {
    queryParams.active_only = params.activeOnly ? "true" : "false";
  }
  const response = await httpGetRequest<TicketDepartmentsResponse>(
    APIUrlGenerator(API_ROUTES.GET_TICKET_DEPARTMENTS, queryParams)
  );
  return response.data.departments;
};

export const createTicketDepartment = async (
  payload: TicketDepartmentPayload
) => {
  const response = await httpPostRequest<TicketDepartmentResponse>(
    APIUrlGenerator(API_ROUTES.CREATE_TICKET_DEPARTMENT),
    payload
  );
  return response.data.department;
};

export const updateTicketDepartment = async (
  id: string | number,
  payload: TicketDepartmentPayload
) => {
  const response = await httpPutRequest<TicketDepartmentResponse>(
    APIUrlGenerator(API_ROUTES.UPDATE_TICKET_DEPARTMENT(id.toString())),
    payload
  );
  return response.data.department;
};

export const deleteTicketDepartment = async (id: string | number) => {
  const response = await httpDeleteRequest<{ message: string }>(
    APIUrlGenerator(API_ROUTES.DELETE_TICKET_DEPARTMENT(id.toString()))
  );
  return response.data;
};

export const getTicketStatuses = async (params?: { activeOnly?: boolean }) => {
  const queryParams: Record<string, string | number | null> = {};
  if (typeof params?.activeOnly === "boolean") {
    queryParams.active_only = params.activeOnly ? "true" : "false";
  }
  const response = await httpGetRequest<TicketStatusesResponse>(
    APIUrlGenerator(API_ROUTES.GET_TICKET_STATUSES, queryParams)
  );
  return response.data.statuses;
};

export const createTicketStatus = async (payload: TicketStatusPayload) => {
  const response = await httpPostRequest<TicketStatusResponse>(
    APIUrlGenerator(API_ROUTES.CREATE_TICKET_STATUS),
    payload
  );
  return response.data.status;
};

export const updateTicketStatusConfig = async (
  id: string | number,
  payload: TicketStatusPayload
) => {
  const response = await httpPutRequest<TicketStatusResponse>(
    APIUrlGenerator(API_ROUTES.UPDATE_TICKET_STATUS_CONFIG(id.toString())),
    payload
  );
  return response.data.status;
};

export const deleteTicketStatus = async (id: string | number) => {
  const response = await httpDeleteRequest<{ message: string }>(
    APIUrlGenerator(API_ROUTES.DELETE_TICKET_STATUS(id.toString()))
  );
  return response.data;
};

export const getTicketSubjects = async (params?: {
  activeOnly?: boolean;
  departmentId?: number;
}) => {
  const queryParams: Record<string, string | number | null> = {};
  if (typeof params?.activeOnly === "boolean") {
    queryParams.active_only = params.activeOnly ? "true" : "false";
  }
  if (params?.departmentId) {
    queryParams.department_id = params.departmentId;
  }
  const response = await httpGetRequest<TicketSubjectsResponse>(
    APIUrlGenerator(API_ROUTES.GET_TICKET_SUBJECTS, queryParams)
  );
  return response.data.subjects;
};

export const createTicketSubject = async (payload: TicketSubjectPayload) => {
  const response = await httpPostRequest<TicketSubjectResponse>(
    APIUrlGenerator(API_ROUTES.CREATE_TICKET_SUBJECT),
    payload
  );
  return response.data.subject;
};

export const updateTicketSubject = async (
  id: string | number,
  payload: TicketSubjectPayload
) => {
  const response = await httpPutRequest<TicketSubjectResponse>(
    APIUrlGenerator(API_ROUTES.UPDATE_TICKET_SUBJECT(id.toString())),
    payload
  );
  return response.data.subject;
};

export const deleteTicketSubject = async (id: string | number) => {
  const response = await httpDeleteRequest<{ message: string }>(
    APIUrlGenerator(API_ROUTES.DELETE_TICKET_SUBJECT(id.toString()))
  );
  return response.data;
};


