import axiosInstance from "@/shared/services/axiosInstance";
import recordAxiosInstance from "@/shared/services/recordAxiosInstance";
import { RemarkParams } from "@/super-admin/hooks/useRemark";
import { AxiosResponse } from "axios";
export interface PrefixSuffixName {
  am: string;
  en: string;
}

export interface CreatePrefixPayload {
  name: PrefixSuffixName;
  unitId: string;
  positionId?: string;
  isForCC: boolean;
  recordTypeKey?: string | null;
  recordTagId?: string;
  count?: number; // Only used for prefixes, ignored for suffixes
}

export interface CreateSuffixPayload {
  name: PrefixSuffixName;
  unitId: string;
  isForCC: boolean;
  recordTypeKey: string;
}
export interface InternalPrefixSuffixName {
  am: string;
  en: string;
}

export enum EEscalationNotificationType {
  JUMP = "JUMP",
  NOTIFY_STEP = "NOTIFY_STEP",
  NOTIFY_PARENT_STEP = "NOTIFY_PARENT_STEP",
}

export enum EEscalationNotificationChannel {
  ALL = "ALL",
  EMAIL = "EMAIL",
  IN_APP = "IN_APP",
  SMS = "SMS",
}

export enum ERecordSenderInformationScope {
  NAME = "NAME",
  NAME_AND_SIGNATURE = "NAME_AND_SIGNATURE",
  NAME_AND_STAMP = "NAME_AND_STAMP",
  SIGNATURE_AND_STAMP = "SIGNATURE_AND_STAMP",
  SIGNATURE = "SIGNATURE",
  STAMP = "STAMP",
  ALL = "ALL",
}

export interface UnitConfigurationPayload {
  isMultipleDelegationAllowed?: boolean;
  waitAllCollaboratorsBeforeAction?: boolean;
  forwardWithTeeterSignature?: boolean;
  shouldCollaboratorAlwaysSign?: boolean;
  shouldIncludeForYourReferenceInCC?: boolean;
  canCollaborationBeCreatedForAll?: boolean;
  canCreateDirectRecord?: boolean;
  canCreateBankRelatedRecords?: boolean;
  isApprovalRequiredForPositionChange?: boolean;
  canPositionBasedRecordsBeCreated?: boolean;
  canPositionBasedReferenceBeUsedForAllTypes?: boolean;
  shouldDirectRecordHaveHeaderAndFooter?: boolean;
  canAssignCustomReferenceNumber?: boolean;
  canInternalMemosHaveSenderInformation?: boolean;
  // Complaint receiving eligibility for the unit.
  canReceiveComplaint?: boolean;
  recordSenderInformationScope: ERecordSenderInformationScope;
  canRecordDateLabelHasLocalization?: boolean;
  canRecordDateHaveTime?: boolean;
  canInternalMemoRequirePositionBasedReferenceNumber?: boolean;
  internalSuffix?: InternalPrefixSuffixName;
  internalPrefix?: InternalPrefixSuffixName;
  internalSuffixCC?: InternalPrefixSuffixName;
  internalPrefixCC?: InternalPrefixSuffixName;
  referenceNumberPrefix?: string;
  externalReferenceNumberPrefix?: string;
  internalMemoReferenceNumberPrefix?: string;
  positionScopeToFetch?: string;
  escalationHour?: number;
  urgentLetterEscalationHour?: number;
  onReviewLetterEscalationHour?: number;
  urgentOnReviewLetterEscalationHour?: number;
  escalationNotificationType?: EEscalationNotificationType;
  escalationNotificationChannel?: EEscalationNotificationChannel;
  unitId: string;
  attachSignatureOnAttachment?: boolean;
  canHaveTagBasedReferenceNumber?: boolean;
  referenceNumberCountFirst?: boolean;
  canUserSkipLevel?: boolean;
  canROReturn?: boolean;
  canRecordBeEditedByCollaborators?: boolean;
  canRecordHaveCollaboratorsName?: boolean;
  allowEditOnCollaboration?: boolean;
  canRecordBeEditedOnWorkflow?: boolean;
  canIncomingAssignmentsBeSeparated?: boolean;
  internalMemoHasReferenceNumber?: boolean;
  canSendToBranch?: boolean;
}

export interface UnitConfigurationItem extends UnitConfigurationPayload {
  canSendToBranch: boolean;
  id: string;
  createdAt?: string;
  updatedAt?: string;
}

export interface UnitConfigurationListResponse {
  items: UnitConfigurationItem[];
  count?: number;
}

export interface RemarkPayload {
  unitId: string;
  remark: string;
  description?: string;
}

export interface ExternalReferenceNumberPrefixDTO {
  externalReferenceNumberPrefix: string;
}
export interface InternalMemoReferenceNumberPrefixDTO {
  internalMemoReferenceNumberPrefix: string;
}

export interface PrefixSuffixDto {
  id: string;
  name: PrefixSuffixName;
  createdAt: string;
  updatedAt: string;
  recordTypeKey: string;
  isForCC?: boolean;
  positionId?: string;
  recordSequences?: {
    createdAt: string;
    updatedAt: string;
    id: string;
    count: number;
    year: number;
    unitId: string;
    recordPrefixId: string;
    recordSequenceType: string;
  }[];
}

export interface PrefixSuffixListResponseDto {
  items: PrefixSuffixDto[];
  count: number;
}

export interface PrefixRefPayloadDto {
  referenceNumberPrefix: string;
}
export interface UnitDto {
  createdAt: string; // ISO date string
  updatedAt: string; // ISO date string
  id: string; // UUID
  name: {
    am: string; // Amharic name
    en: string; // English name
  };
  key: string; // Unique identifier/key
  referenceNumberPrefix: string; // Prefix for reference numbers
  organizationId: string; // UUID of the parent organization
  parentUnitId: string | null; // UUID of parent unit or null if root unit
}

export const prefixSuffixService = {
  createPrefix: async (
    payload: CreatePrefixPayload,
  ): Promise<AxiosResponse> => {
    return recordAxiosInstance.post("/record-prefixes", payload);
  },

  updateCount: async (
    sequenceId: string,
    count: number,
  ): Promise<AxiosResponse<PrefixSuffixListResponseDto>> => {
    return recordAxiosInstance.put(`/record-sequences/${sequenceId}`, {
      count,
    });
  },
  getPrefix: async (
    unitId: string,
    recordTypeKey: string,
    skip: number = 0,
    take: number = 10,
  ): Promise<AxiosResponse<PrefixSuffixListResponseDto>> => {
    return recordAxiosInstance.get(
      `/record-prefixes/list/${unitId}/${recordTypeKey}`,
      {
        params: {
          skip,
          take,
          orderBy: "createdAt:DESC",
        },
      },
    );
  },
  getPrefixByRecordTagId: async (
    unitId: string,
    recordTagId: string,
    skip: number = 0,
    take: number = 10,
  ): Promise<AxiosResponse<PrefixSuffixListResponseDto>> => {
    return recordAxiosInstance.get(
      `/record-prefixes/list/${unitId}/by-tag/${recordTagId}`,
      {
        params: {
          skip,
          take,
          orderBy: "createdAt:DESC",
        },
      },
    );
  },
  getPrefixById: async (
    id: string,
  ): Promise<AxiosResponse<PrefixSuffixDto>> => {
    return recordAxiosInstance.get(`/record-prefixes/list/${id}`);
  },
  getPrefixCC: async (
    unitId: string,
    recordTypeKey: string,
    skip: number = 0,
    take: number = 10,
  ): Promise<AxiosResponse<PrefixSuffixListResponseDto>> => {
    return recordAxiosInstance.get(
      `/record-prefixes/list/cc/${unitId}/${recordTypeKey}`,
      {
        params: {
          skip,
          take,
          orderBy: "createdAt:DESC",
        },
      },
    );
  },
  deletePrefix: async (Id: string): Promise<AxiosResponse> => {
    return recordAxiosInstance.delete(`/record-prefixes/${Id}`);
  },
  editPrefix: async (
    id: string,
    payload: CreatePrefixPayload,
  ): Promise<AxiosResponse> => {
    return recordAxiosInstance.put(`/record-prefixes/${id}`, payload);
  },
  createSuffix: async (
    payload: CreateSuffixPayload,
  ): Promise<AxiosResponse> => {
    return recordAxiosInstance.post("/record-suffixes", payload);
  },
  getSuffix: async (
    unitId: string,
    recordTypeKey: string,
    skip: number = 0,
    take: number = 10,
  ): Promise<AxiosResponse<PrefixSuffixListResponseDto>> => {
    return recordAxiosInstance.get(
      `/record-suffixes/list/${unitId}/${recordTypeKey}`,
      {
        params: {
          skip,
          take,
          orderBy: "createdAt:DESC",
        },
      },
    );
  },
  getPrefixByPosition: async (
    unitId: string,
    recordTypeKey: string,
    skip: number = 0,
    take: number = 10,
  ): Promise<AxiosResponse<PrefixSuffixListResponseDto>> => {
    return recordAxiosInstance.get(
      `/record-prefixes/list/by-position/${unitId}/${recordTypeKey}`,
      {
        params: {
          skip,
          take,
          orderBy: "createdAt:DESC",
        },
      },
    );
  },
  getSuffixById: async (
    id: string,
  ): Promise<AxiosResponse<PrefixSuffixDto>> => {
    return recordAxiosInstance.get(`/record-suffixes/list/${id}`);
  },
  getSuffixCC: async (
    unitId: string,
    recordTypeKey: string,
    skip: number = 0,
    take: number = 10,
  ): Promise<AxiosResponse<PrefixSuffixListResponseDto>> => {
    return recordAxiosInstance.get(
      `/record-suffixes/list/cc/${unitId}/${recordTypeKey}`,
      {
        params: {
          skip,
          take,
          orderBy: "createdAt:DESC",
        },
      },
    );
  },
  deleteSuffix: async (Id: string): Promise<AxiosResponse> => {
    return recordAxiosInstance.delete(`/record-suffixes/${Id}`);
  },
  editSuffix: async (
    id: string,
    payload: CreateSuffixPayload,
  ): Promise<AxiosResponse> => {
    return recordAxiosInstance.put(`/record-suffixes/${id}`, payload);
  },
  createRef: async (
    payload: PrefixRefPayloadDto,
    unitId: string,
  ): Promise<AxiosResponse> => {
    return axiosInstance.patch(
      `/units/add-reference-number-prefix/${unitId}`,
      payload,
    );
  },
  deleteRef: async (Id: string): Promise<AxiosResponse> => {
    return recordAxiosInstance.delete(`/record-suffixes/${Id}`);
  },
  getRef: async (Id: string): Promise<AxiosResponse> => {
    return axiosInstance.get(`/units/${Id}`);
  },
  createInternalPrefixSuffix: async (
    payload: Partial<UnitConfigurationPayload>,
  ): Promise<AxiosResponse> => {
    return axiosInstance.post(`/unit-configurations`, payload);
  },
  getInternalConfig: async (
    unitId: string,
  ): Promise<AxiosResponse<UnitConfigurationListResponse>> => {
    return axiosInstance.get(`/unit-configurations/list/${unitId}`);
  },
  updateInternalPrefixSuffix: async (
    payload: Partial<UnitConfigurationPayload>,
    id: string,
  ): Promise<AxiosResponse> => {
    return axiosInstance.put(`/unit-configurations/${id}`, payload);
  },
  deleteInternalPrefixSuffix: async (id: string): Promise<AxiosResponse> => {
    return axiosInstance.delete(`/unit-configurations/${id}`);
  },
  createRemark: async (payload: RemarkPayload): Promise<AxiosResponse> => {
    return recordAxiosInstance.post(`/record-common-remarks`, payload);
  },
  getRemarkList: async (
    unitId: string,
    params: RemarkParams,
  ): Promise<AxiosResponse> => {
    return recordAxiosInstance.get(`/record-common-remarks/list/${unitId}/`, {
      params,
    });
  },
  editRemark: async (
    id: string,
    payload: RemarkPayload,
  ): Promise<AxiosResponse> => {
    return recordAxiosInstance.put(`/record-common-remarks/${id}`, payload);
  },
  deleteRemarks: async (id: string): Promise<AxiosResponse> => {
    return recordAxiosInstance.delete(`/record-common-remarks/${id}`);
  },
  editExternalReferencePrefix: async (
    id: string,
    payload: ExternalReferenceNumberPrefixDTO,
  ): Promise<AxiosResponse> => {
    return axiosInstance.patch(
      `/unit-configurations/add-external-reference-number-prefix/${id}`,
      payload,
    );
  },
  editInternalMemoReferencePrefix: async (
    id: string,
    payload: InternalMemoReferenceNumberPrefixDTO,
  ): Promise<AxiosResponse> => {
    return axiosInstance.patch(
      `/unit-configurations/add-internal-memo-reference-number-prefix/${id}`,
      payload,
    );
  },
};
