import recordAxiosInstance from "@/shared/services/recordAxiosInstance";
import { withHeaders } from "./withHeaders";

export interface RecordCollaboratorCommentDto {
  id: string;
  recordId?: string;
  comment: string;
  recordCollaboratorId?: string;
  recordContentId?: string;
  createdAt: string;
  updatedAt: string;
  amharicCreatedAt: string | null;
  amharicUpdatedAt: string | null;
  recordCollaborator?: {
    id: string;
    employeePosition?: {
      id: string;
      employee?: {
        id: string;
        name: {
          am: string;
          en: string;
        };
      };
    };
  };
}

export interface RecordCollaboratorCommentsListResponse {
  count: number;
  items: RecordCollaboratorCommentDto[];
}

export interface CreateRecordCollaboratorCommentPayload {
  recordId: string;
  comment: string;
}

export interface UpdateRecordCollaboratorCommentPayload {
  comment: string;
}

export interface RecordCollaboratorCommentsListParams {
  skip?: number;
  take?: number;
  order?: string;
}

export interface DeleteRecordCollaboratorCommentResponse {
  affected: number;
}

export const createRecordCollaboratorComment = async (
  payload: CreateRecordCollaboratorCommentPayload,
): Promise<RecordCollaboratorCommentDto> => {
  const { data } = await recordAxiosInstance.post<RecordCollaboratorCommentDto>(
    "/record-collaborator-comments",
    payload,
    {
      headers: withHeaders(),
    },
  );

  return data;
};

export const updateRecordCollaboratorComment = async (
  id: string,
  payload: UpdateRecordCollaboratorCommentPayload,
): Promise<RecordCollaboratorCommentDto> => {
  const { data } = await recordAxiosInstance.put<RecordCollaboratorCommentDto>(
    `/record-collaborator-comments/${id}`,
    payload,
    {
      headers: withHeaders(),
    },
  );

  return data;
};

export const getMyRecordCollaboratorComments = async (
  recordCollaboratorId: string,
): Promise<RecordCollaboratorCommentDto[]> => {
  const { data } = await recordAxiosInstance.get<
    RecordCollaboratorCommentDto[]
  >(`/record-collaborator-comments/my-comments/${recordCollaboratorId}`, {
    headers: withHeaders(),
  });

  return data;
};

export const listRecordCollaboratorComments = async (
  contentId: string,
  params?: RecordCollaboratorCommentsListParams,
): Promise<RecordCollaboratorCommentsListResponse> => {
  const { data } =
    await recordAxiosInstance.get<RecordCollaboratorCommentsListResponse>(
      `/record-collaborator-comments/list/${contentId}`,
      {
        headers: withHeaders(),
        params,
      },
    );

  return data;
};

export const getRecordCollaboratorCommentById = async (
  id: string,
): Promise<RecordCollaboratorCommentDto> => {
  const { data } = await recordAxiosInstance.get<RecordCollaboratorCommentDto>(
    `/record-collaborator-comments/${id}`,
    {
      headers: withHeaders(),
    },
  );

  return data;
};

export const deleteRecordCollaboratorComment = async (
  id: string,
): Promise<DeleteRecordCollaboratorCommentResponse> => {
  const { data } =
    await recordAxiosInstance.delete<DeleteRecordCollaboratorCommentResponse>(
      `/record-collaborator-comments/${id}`,
      {
        headers: withHeaders(),
      },
    );

  return data;
};
