import recordAxiosInstance from "@/shared/services/recordAxiosInstance";
import Cookies from "js-cookie";
import { withHeaders } from "./withHeaders";

export interface MyCollaborationsQuery {
  skip?: number;
  take?: number;
  orderBy?: string;
  order?: string;
  status?: string;
  signStatus?: string;
  receiverPosition?: string;
  receiverEmployee?: string;
  subject?: string;
  fromCreatedAt?: string;
  toCreatedAt?: string;
  reference?: string;
  letterName?: string;
}

export interface SignRecordCollaboratorPayload {
  collabId: string;
  signStatus:
    | "draft"
    | "signed"
    | "rejected"
    | "team_rejected"
    | "adjustment_requested";
  comment?: string;
}

export interface CollaborationDto {
  amharicSignedAt?: string | null;
  amharicCreatedAt: string;
  id: string;
  createdAt: string;
  // "team_rejected" = collaborator commented (send-back, editable by creator)
  // "rejected"      = collaborator hard-rejected (final)
  signStatus:
    | "draft"
    | "rejected"
    | "signed"
    | "team_rejected"
    | "adjustment_requested";
  signedAt: string | null;
  comment?: string | null;
  employeePositionId: string;
  userId: string;
  employeePosition?: {
    id: string;
    employee?: {
      id: string;
      name?: {
        am?: string;
        en?: string;
      };
    };
    position?: {
      id: string;
      name?: {
        am?: string;
        en?: string;
      };
    };
  };
  record: {
    receivingUnits: any;
    recordTypeKey: any;
    letterName: any;
    referenceNumber: string;
    id: string;
    userId?: string; // The user id of the record's creator/initiator
    statusKey: "Draft" | "Team Rejected" | "Team Signature" | "Team Approved"; // Match backend enum
    content: {
      nonSmartOfficeReceivers: never[];
      receiverEmployees: any;
      receiverPositions: any;
      id: string;
      subject: string;
    }[];
  };
}

export interface CollaborationListResponse /* replace with real DTO later */ {
  count: number;
  items: CollaborationDto[]; // <-- swap with CollaborationDto[]
}

export const fetchCollaborations = async (
  params: MyCollaborationsQuery,
): Promise<CollaborationListResponse> => {
  try {
    const { data } = await recordAxiosInstance.get<CollaborationListResponse>(
      "collaborations", // adjust path when you get the final endpoint
      {
        headers: withHeaders(),
        params,
      },
    );
    return data;
  } catch (error) {
    throw error;
  }
};
export const fetchMyCollaborations = async (
  params?: MyCollaborationsQuery,
): Promise<CollaborationListResponse> => {
  try {
    const { data } = await recordAxiosInstance.get<CollaborationListResponse>(
      "/record-collaborators/my-collaborations", // adjust path when you get the final endpoint
      {
        headers: withHeaders(),
        params,
      },
    );
    return data;
  } catch (error) {
    throw error;
  }
};

export const getCollaborationByRecordId = async (
  id: string,
): Promise<CollaborationListResponse> => {
  try {
    const { data } = await recordAxiosInstance.get<CollaborationListResponse>(
      `/record-collaborators/list/${id}`, // adjust path when you get the final endpoint
      {
        headers: withHeaders(),
      },
    );
    return data;
  } catch (error) {
    throw error;
  }
};
export const signRecordCollaborator = async (
  payload: SignRecordCollaboratorPayload,
): Promise<void> => {
  try {
    await recordAxiosInstance.patch("/record-collaborators/sign", payload, {
      headers: {
        ...withHeaders(),
      },
    });
  } catch (error) {
    throw error;
  }
};
