import { withHeaders } from "@/record-management/services/api/withHeaders";
import axiosInstance from "@/shared/services/axiosInstance";
import { AxiosResponse } from "axios";
import { uploadAndCreateResource } from "@/shared/services/unifiedUploadService";
import { position } from "html2canvas/dist/types/css/property-descriptors/position";

export interface HeaderFooterFileInfo {
  fileName: string;
  contentType: string;
  size: number;
  originalname: string;
}

export interface HeaderFooterName {
  am: string;
  en: string;
}

export interface CreateHeaderFooterPayload {
  fileInfo: HeaderFooterFileInfo;
  name: HeaderFooterName;
  unitId: string;
}

export interface HeaderFooterDto {
  id: string;
  createdAt: string;
  updatedAt: string;
  fileInfo: HeaderFooterFileInfo;
  uploadedSuccessfully: boolean;
  isCurrent: boolean;
  name: HeaderFooterName;
  unitId: string;
  presigned?: string;
}

export interface HeaderFooterResponseDto extends HeaderFooterDto {
  type: "header" | "footer";
  id: string;
}

export interface HeaderFooterListResponse {
  items: HeaderFooterResponseDto[];
  count: number;
}

export interface HeaderFooterHeaders {
  tenantKey: string;
  unitId?: string;
}

export interface HeaderFooterStatusUpdatePayload {
  id: string;
  parentId: string;
  uploadedSuccessfully: true;
}

export interface HeaderFooterChangeStatusPayload {
  isCurrent: boolean;
}

export const headerFooterService = {
  // ======= UPLOAD METHODS =======
  uploadAndCreateHeader: (
    file: File,
    fileName: string,
    unitId: string,
    positionId?: string[],
    recordTypeKey?: string | null,
  ) =>
    uploadAndCreateResource(
      {
        type: "header",
        file,
        name: { am: fileName, en: fileName },
        parentId: unitId,
      },
      axiosInstance,
      positionId,
      recordTypeKey,
    ),

  uploadAndCreateFooter: (
    file: File,
    fileName: string,
    unitId: string,
    positionId?: string[],
    recordTypeKey?: string | null,
  ) =>
    uploadAndCreateResource(
      {
        type: "footer",
        file,
        name: { am: fileName, en: fileName },
        parentId: unitId,
      },
      axiosInstance,
      positionId,
      recordTypeKey,
    ),

  // ======= HEADERS =======
  createHeader: async (
    payload: CreateHeaderFooterPayload,
  ): Promise<AxiosResponse<HeaderFooterResponseDto>> => {
    return axiosInstance.post("/headers", payload, { headers: withHeaders() });
  },

  getHeaders: async (): Promise<AxiosResponse<HeaderFooterListResponse>> => {
    return axiosInstance.get("/headers", { headers: withHeaders() });
  },

  getHeadersByUnitId: async (
    unitId: string,
    positionId?: string,
    recordTypeKey?: string,
  ): Promise<AxiosResponse<HeaderFooterListResponse>> => {
    return axiosInstance.get(
      `/headers/list/${unitId}?take=100${positionId ? `&positionId=${positionId}` : ""}${recordTypeKey ? `&recordTypeKey=${recordTypeKey}` : ""}`,
      {
        headers: withHeaders(),
      },
    );
  },

  getHeaderById: async (
    id: string,
  ): Promise<AxiosResponse<HeaderFooterResponseDto>> => {
    return axiosInstance.get(`/headers/${id}`, { headers: withHeaders() });
  },

  updateHeader: async (
    id: string,
    payload: Partial<CreateHeaderFooterPayload>,
  ): Promise<AxiosResponse<HeaderFooterResponseDto>> => {
    return axiosInstance.put(`/headers/${id}`, payload, {
      headers: withHeaders(),
    });
  },

  deleteHeader: async (id: string): Promise<AxiosResponse<void>> => {
    return axiosInstance.delete(`/headers/${id}`, { headers: withHeaders() });
  },

  updateHeaderUploadStatus: async (
    id: string,
    payload: HeaderFooterStatusUpdatePayload,
  ): Promise<AxiosResponse<HeaderFooterResponseDto>> => {
    return axiosInstance.patch(`/headers/${id}/upload-status`, payload, {
      headers: withHeaders(),
    });
  },

  changeHeaderStatus: async (
    id: string,
    payload: HeaderFooterChangeStatusPayload,
  ): Promise<AxiosResponse<HeaderFooterResponseDto>> => {
    return axiosInstance.put(`/headers/change-status/${id}`, payload, {
      headers: withHeaders(),
    });
  },

  // ======= FOOTERS =======
  createFooter: async (
    payload: CreateHeaderFooterPayload,
  ): Promise<AxiosResponse<HeaderFooterResponseDto>> => {
    return axiosInstance.post("/footers", payload, { headers: withHeaders() });
  },

  getFooters: async (): Promise<AxiosResponse<HeaderFooterListResponse>> => {
    return axiosInstance.get("/footers", { headers: withHeaders() });
  },

  getFootersByUnitId: async (
    unitId: string,
    positionId?: string,
    recordTypeKey?: string,
  ): Promise<AxiosResponse<HeaderFooterListResponse>> => {
    return axiosInstance.get(
      `/footers/list/${unitId}?take=300${positionId ? `&positionId=${positionId}` : ""}${recordTypeKey ? `&recordTypeKey=${recordTypeKey}` : ""}`,
      {
        headers: withHeaders(),
      },
    );
  },

  getFooterById: async (
    id: string,
  ): Promise<AxiosResponse<HeaderFooterResponseDto>> => {
    return axiosInstance.get(`/footers/${id}`, { headers: withHeaders() });
  },

  updateFooter: async (
    id: string,
    payload: Partial<CreateHeaderFooterPayload>,
  ): Promise<AxiosResponse<HeaderFooterResponseDto>> => {
    return axiosInstance.put(`/footers/${id}`, payload, {
      headers: withHeaders(),
    });
  },

  deleteFooter: async (id: string): Promise<AxiosResponse<void>> => {
    return axiosInstance.delete(`/footers/${id}`, { headers: withHeaders() });
  },

  updateFooterUploadStatus: async (
    id: string,
    payload: HeaderFooterStatusUpdatePayload,
  ): Promise<AxiosResponse<HeaderFooterResponseDto>> => {
    return axiosInstance.patch(`/footers/${id}/upload-status`, payload, {
      headers: withHeaders(),
    });
  },

  changeFooterStatus: async (
    id: string,
    payload: HeaderFooterChangeStatusPayload,
  ): Promise<AxiosResponse<HeaderFooterResponseDto>> => {
    return axiosInstance.put(`/footers/change-status/${id}`, payload, {
      headers: withHeaders(),
    });
  },
};
