import axios, { AxiosInstance, AxiosResponse } from 'axios';
import { ReportMissingBody } from '@/dto/missing.dto';

export interface ServiceOptions {
  headers?: Record<string, unknown>;
  params?: Record<string, unknown>;
}

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

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

  return instance;
};

const reportPermanentlyMissing = (
  trackingId: string,
  body: ReportMissingBody,
): Promise<AxiosResponse> => {
  return API().put(`/report-permanently-missing/${trackingId}`, body);
};

const getTrackingDetail = (trackingId: string): Promise<AxiosResponse> => {
  return API().get(`/${trackingId}`);
};

export default {
  reportPermanentlyMissing,
  getTrackingDetail,
};
