import api from 'src/config/api';
import {
  FolderResponse,
  DocumentResponse,
  FolderListResponse,
  DocumentListResponse,
  SignatureStatus,
  DocumentSignSubmitData,
  DocumentNotifications,
} from 'src/types/documents';

export const getDocumentList = (
  page,
  limit,
  folderId?: number,
  signatureStatus?: string,
) =>
  api.get<DocumentListResponse>('/documents/files', {
    params: {
      folderId,
      signatureStatus,
      page,
      limit,
      order: 'DESC',
      orderBy: 'CREATED_AT',
    },
  });

export const getPendingDocumentList = (
  page: number = 1,
  limit: number = 1,
  folderId?: number,
) =>
  api.get<DocumentListResponse>('/documents/files', {
    params: {
      folderId,
      signatureStatus: SignatureStatus.PENDING,
      page,
      limit,
      order: 'ASC',
      orderBy: 'CREATED_AT',
    },
  });

export const signDocument = ({
  id,
  signatureUrl,
  inDisagreement,
}: DocumentSignSubmitData) =>
  api.put(`documents/files/${id}/signature`, { signatureUrl, inDisagreement });

export const getFolderList = (page: number = 1, limit: number = 1) =>
  api.get<FolderListResponse>('documents/folders', { params: { page, limit } });

export const getFolder = (id: number) =>
  api.get<FolderResponse>(`documents/folders/${id}`);

export const getDocument = (id: number) =>
  api.get<DocumentResponse>(`documents/files/${id}`);

export const getDocumentNotifications = () =>
  api.get<DocumentNotifications>('red-bubbles/documents');

export const clearFolderNotifications = (id: number) =>
  api.post(`red-bubbles/documents/${id}/clear`);

export const documentLogin = (password: string) =>
  api.post('/auth/check-password', {
    password,
  });
