import {
  UploadSignatureDto,
  UploadTeeterDto,
} from "@/record-management/dto/fileDto";
import { uploadAndCreateResource } from "@/shared/services/unifiedUploadService";
import axiosInstance from "@/shared/services/axiosInstance";

import {
  SignatureListResponse,
  StampResponseDTO,
  TeeterListResponse,
} from "@/record-management/dto/userRecords/teetersAndSignatureDto";
import Cookies from "js-cookie";

export const withHeaders = (passPosId: boolean = false) => {
  const unitId = Cookies.get("unit-id");
  const positionId = Cookies.get("current-position-id");
  const projectId = Cookies.get("current-project-id");
  const delegatedPositionId = Cookies.get("delegatedPositionId");

  const headers: Record<string, string> = {};

  if (unitId) headers["unit-id"] = unitId;
  if (projectId) headers["x-current-project-id"] = projectId;
  if (passPosId && delegatedPositionId)
    headers["x-delegated-position-id"] = delegatedPositionId;
  if (passPosId && positionId) headers["x-current-position-id"] = positionId;
  return headers;
};

export const SignatureAndTeeterService = {
  signature: {
    uploadAndCreate: (file: File, fileName: string, employeeId: string) =>
      uploadAndCreateResource(
        {
          type: "signature",
          file,
          name: { am: fileName, en: fileName },
          parentId: employeeId,
        },
        axiosInstance,
      ),
  },

  teeter: {
    uploadAndCreate: (
      file: File,
      fileName: string,
      employeePositionId: string,
      locale?: "am" | "en" | null,
    ) =>
      uploadAndCreateResource(
        {
          type: "stamp",
          file,
          name: { am: fileName, en: fileName },
          parentId: employeePositionId,
          locale,
        },
        axiosInstance,
      ),
  },
};
export const fetchSignatureList = async (
  id: string,
): Promise<SignatureListResponse> => {
  try {
    const { data } = await axiosInstance.get<SignatureListResponse>(
      `/signatures/mine`,
      {
        headers: {
          ...withHeaders(),
        },
      },
    );

    return data;
  } catch (error) {
    throw error;
  }
};

export const fetchTeetersList = async (
  id: string,
): Promise<TeeterListResponse> => {
  try {
    const { data } = await axiosInstance.get<TeeterListResponse>(
      `/employee-stamps/list/${id}`,
      {
        headers: {
          ...withHeaders(true),
        },
      },
    );

    return data;
  } catch (error) {
    throw error;
  }
};
export const fetchMyTeetersList = async (): Promise<StampResponseDTO> => {
  const { data } = await axiosInstance.get<StampResponseDTO>(
    `/employee-stamps/mine`,
    { headers: { ...withHeaders(true) } },
  );
  return data;
};

export const fetchSignaturePresigned = async (
  id: string,
): Promise<{ presigned: string }> => {
  try {
    const { data } = await axiosInstance.get(`/signatures/${id}`, {
      headers: withHeaders(),
    });
    return data;
  } catch (error) {
    throw error;
  }
};

export const fetchTeeterPresigned = async (
  id: string,
): Promise<{ presigned: string }> => {
  try {
    const { data } = await axiosInstance.get(`/employee-stamps/${id}`, {
      headers: withHeaders(),
    });
    return data;
  } catch (error) {
    throw error;
  }
};

export const deactivateSignature = async (id: string) => {
  await axiosInstance.put(
    `/signatures/change-status/${id}`,
    {
      isCurrent: false,
    },
    {
      headers: withHeaders(),
    },
  );
};

export const deactivateTeeter = async (id: string) => {
  await axiosInstance.put(
    `/employee-stamps/change-status/${id}`,
    {
      isCurrent: false,
    },
    {
      headers: withHeaders(),
    },
  );
};
