import { withHeaders } from "@/record-management/services/api/withHeaders";
import axiosInstance from "@/shared/services/axiosInstance";
import { AxiosResponse } from "axios";

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

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

export interface CreateSealPayload {
  fileInfo: SealFileInfo;
  name: SealName;
  unitId: string;
}

export interface SealDto {
  id: string;
  createdAt: string;
  updatedAt: string;
  fileInfo: SealFileInfo;
  uploadedSuccessfully: boolean;
  isCurrent: boolean;
  name: SealName;
  unitId: string;
  presigned?: string;
}

export interface SealResponseDto {
  id: string;
  unitId: string;
  isCurrent: boolean;
  uploadedSuccessfully: boolean;
  createdAt: string;
  updatedAt: string;
  name: SealName;
  fileInfo: SealFileInfo;
  presigned: string;
}

export interface SealListResponse {
  items: SealResponseDto[];
  count: number;
}

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

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

export interface SealChangeStatusPayload {
  isCurrent: boolean;
}

export const sealService = {
  createSeal: async (
    payload: CreateSealPayload
  ): Promise<AxiosResponse<SealResponseDto>> => {
    return axiosInstance.post("/seals", payload, { headers: withHeaders() });
  },

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

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

  getSealListsByUnitId: async (
    unitId: string
  ): Promise<AxiosResponse<SealListResponse>> => {
    return axiosInstance.get(`/seals/list/${unitId}`, {
      headers: withHeaders(),
    });
  },

  updateSeal: async (
    id: string,
    payload: Partial<CreateSealPayload>
  ): Promise<AxiosResponse<SealResponseDto>> => {
    return axiosInstance.put(`/seals/${id}`, payload, {
      headers: withHeaders(),
    });
  },

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

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

  updateSealUploadStatus: async (
    id: string,
    updateStatusPayload: SealStatusUpdatePayload
  ): Promise<AxiosResponse<SealResponseDto>> => {
    return axiosInstance.patch(
      `/seals/${id}/upload-status`,
      updateStatusPayload,
      { headers: withHeaders() }
    );
  },
};
