import axios, { AxiosInstance, AxiosResponse } from 'axios';
import { ReportDamageBody } from '@/dto/damage.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/damage-repair-ticketing/v2/damage`,
    headers: {
      Authorization: `Bearer ${user.token}`,
      ...headers,
    },
    params,
  });

  return instance;
};

const reportDamage = (
  id: string,
  body: ReportDamageBody,
): Promise<AxiosResponse> => {
  const headers = { 'Content-Type': 'multipart/form-data' };
  return API({ headers }).put(`/report-damage/${id}`, body);
};

export default {
  reportDamage,
};
