import { QueryParams } from '@/components/v2/DataTable/DataTable.vue';
import axios, { AxiosInstance, AxiosResponse } from 'axios';

export interface ReportAssetData {
  asset: string;
  notes: string;
  reason: 'Missing';
}

export type DisposalReason = 'Damaged' | 'Missing' | 'No Salvage Value';

export interface ReportDisposalBody {
  data: ReportAssetData[];
}

interface CancelReportDisposalQueryParams {
  isFromDisposal: boolean;
  assetIds: string; // Stringify asset id array
}

export const API = ({ headers = {}, params = {} } = {}): AxiosInstance => {
  const user = JSON.parse(localStorage.getItem('user') as string) ?? {};
  const BASE_URL = import.meta.env.VITE_APP_TAGSAMURAI_API;

  const instance = axios.create({
    baseURL: `${BASE_URL}/fam/disposal/v2`,
    headers: {
      'Content-type': 'application/json',
      'Authorization': `Bearer ${user.token}`,
      ...headers,
    },
    params,
  });

  return instance;
};

const DisposalServices = {
  getReportedDisposal: (params?: QueryParams): Promise<AxiosResponse> => {
    return API({ params }).get('/report');
  },

  postReportDisposal: (body: ReportDisposalBody): Promise<AxiosResponse> => {
    return API().post('/report', body);
  },

  cancelReportDisposal: (
    params: CancelReportDisposalQueryParams,
  ): Promise<AxiosResponse> => {
    return API({ params }).delete('/report/cancel-report');
  },
};

export default DisposalServices;
