import api from './api';
import { Cliente, Location, Risorsa, Articolo, LookupItem } from '../types';

export const lookupService = {
  getTipiEvento: async () => {
    const { data } = await api.get<LookupItem[]>('/lookup/tipi-evento');
    return data;
  },

  getTipiPasto: async () => {
    const { data } = await api.get<LookupItem[]>('/lookup/tipi-pasto');
    return data;
  },

  getTipiOspite: async () => {
    const { data } = await api.get<LookupItem[]>('/lookup/tipi-ospite');
    return data;
  },

  getTipiRisorsa: async () => {
    const { data } = await api.get<LookupItem[]>('/lookup/tipi-risorsa');
    return data;
  },

  getTipiMateriale: async () => {
    const { data } = await api.get<LookupItem[]>('/lookup/tipi-materiale');
    return data;
  },

  getCategorie: async () => {
    const { data } = await api.get<LookupItem[]>('/lookup/categorie');
    return data;
  },

  getClienti: async () => {
    const { data } = await api.get<LookupItem[]>('/lookup/clienti');
    return data;
  },

  getLocation: async () => {
    const { data } = await api.get<LookupItem[]>('/lookup/location');
    return data;
  },

  getRisorse: async () => {
    const { data } = await api.get<LookupItem[]>('/lookup/risorse');
    return data;
  },

  getArticoli: async () => {
    const { data } = await api.get<LookupItem[]>('/lookup/articoli');
    return data;
  },
};

export const clientiService = {
  getAll: async (params?: { search?: string; attivo?: boolean }) => {
    const { data } = await api.get<Cliente[]>('/clienti', { params });
    return data;
  },
  getById: async (id: number) => {
    const { data } = await api.get<Cliente>(`/clienti/${id}`);
    return data;
  },
  create: async (cliente: Partial<Cliente>) => {
    const { data } = await api.post<Cliente>('/clienti', cliente);
    return data;
  },
  update: async (id: number, cliente: Partial<Cliente>) => {
    await api.put(`/clienti/${id}`, { ...cliente, id });
  },
  delete: async (id: number) => {
    await api.delete(`/clienti/${id}`);
  },
};

export const locationService = {
  getAll: async (params?: { search?: string; attivo?: boolean }) => {
    const { data } = await api.get<Location[]>('/location', { params });
    return data;
  },
  getById: async (id: number) => {
    const { data } = await api.get<Location>(`/location/${id}`);
    return data;
  },
  create: async (location: Partial<Location>) => {
    const { data } = await api.post<Location>('/location', location);
    return data;
  },
  update: async (id: number, location: Partial<Location>) => {
    await api.put(`/location/${id}`, { ...location, id });
  },
  delete: async (id: number) => {
    await api.delete(`/location/${id}`);
  },
};

export const risorseService = {
  getAll: async (params?: { search?: string; tipoRisorsaId?: number; attivo?: boolean }) => {
    const { data } = await api.get<Risorsa[]>('/risorse', { params });
    return data;
  },
  getById: async (id: number) => {
    const { data } = await api.get<Risorsa>(`/risorse/${id}`);
    return data;
  },
  create: async (risorsa: Partial<Risorsa>) => {
    const { data } = await api.post<Risorsa>('/risorse', risorsa);
    return data;
  },
  update: async (id: number, risorsa: Partial<Risorsa>) => {
    await api.put(`/risorse/${id}`, { ...risorsa, id });
  },
  delete: async (id: number) => {
    await api.delete(`/risorse/${id}`);
  },
};

export const articoliService = {
  getAll: async (params?: { search?: string; tipoMaterialeId?: number; categoriaId?: number; attivo?: boolean }) => {
    const { data } = await api.get<Articolo[]>('/articoli', { params });
    return data;
  },
  getById: async (id: number) => {
    const { data } = await api.get<Articolo>(`/articoli/${id}`);
    return data;
  },
  create: async (articolo: Partial<Articolo>) => {
    const { data } = await api.post<Articolo>('/articoli', articolo);
    return data;
  },
  update: async (id: number, articolo: Partial<Articolo>) => {
    await api.put(`/articoli/${id}`, { ...articolo, id });
  },
  delete: async (id: number) => {
    await api.delete(`/articoli/${id}`);
  },
  getDisponibilita: async (id: number, data: string) => {
    const response = await api.get(`/articoli/${id}/disponibilita`, { params: { data } });
    return response.data;
  },
};
