import axios, { AxiosInstance, AxiosResponse } from 'axios';

export type GetServiceCenterQueryParams = {
  category: string;
  isActive: string;
};

interface GetServiceCenterResponse {
  status: number;
  message: string;
  data: ResponseData;
}

interface ResponseData {
  totalRecords: number;
  data: ServiceCenter[];
}

interface ServiceCenter {
  _id: string;
  isActive: boolean;
  name: string;
  city: string;
  category: Category[];
  brand: Category[];
  modifiedBy: ModifiedBy;
  updatedAt: string;
}

interface ModifiedBy {
  _id: string;
  fullName: string;
  key: number;
}

interface Category {
  _id: string;
  name: string;
  key: number;
}

export const API = ({ headers = {}, params = {} } = {}): AxiosInstance => {
  const user = JSON.parse(localStorage.getItem('user') ?? '{}');

  const instance = axios.create({
    baseURL: import.meta.env.VITE_APP_TAGSAMURAI_API + '/fam/routine/v2/',
    headers: {
      'Content-type': 'application/json',
      'Authorization': `Bearer ${user.token}`,
      ...headers,
    },
    params,
  });

  return instance;
};

const RoutineServices = {
  getServiceCenter: (
    params: GetServiceCenterQueryParams,
  ): Promise<AxiosResponse<GetServiceCenterResponse>> => {
    return API({ params }).get('service-center');
  },
};

export default RoutineServices;
