import { ref } from "vue";
import type { Granelera } from "~/types/Granelera";
import type { ApiResponse } from "~/types/ApiResponse";

export const useGranelera = () => {
  const loading = ref(false);
  const error = ref<string | null>(null);

  const obtenerGraneleras = async () => {
    try {
      loading.value = true;
      error.value = null;

      const { data, success, message } = await useApiFetch<ApiResponse<Granelera[]>>(
        "/supervisor/granelera",
        {
          method: "POST",
          body: {
            action: "index",
          },
        }
      );

      if (!success) {
        throw new Error(message || "Error al obtener graneleras");
      }

      return data ?? [];
    } catch (err: any) {
      error.value = err instanceof Error ? err.message : "Error al obtener graneleras";
      throw err;
    } finally {
      loading.value = false;
    }
  };

  const crearGranelera = async (payload: { codigo: string; codigo_camion: string; placa: string }) => {
    try {
      loading.value = true;
      error.value = null;

      const body = {
        action: "store",
        ...payload,
      };

      const response = await useApiFetch<ApiResponse<Granelera>>(
        "/supervisor/granelera",
        {
          method: "POST",
          body,
        }
      );

      if (!response.success) {
        throw new Error(response.message || "Error al crear granelera");
      }

      return response;
    } catch (err: any) {
      error.value = err instanceof Error ? err.message : "Error al crear granelera";
      throw err;
    } finally {
      loading.value = false;
    }
  };

  const editarGranelera = async (payload: { granelera_id: number; codigo: string; codigo_camion: string; placa: string }) => {
    try {
      loading.value = true;
      error.value = null;

      const body = {
        action: "update",
        ...payload,
      };

      const response = await useApiFetch<ApiResponse<Granelera>>(
        "/supervisor/granelera",
        {
          method: "POST",
          body,
        }
      );

      if (!response.success) {
        throw new Error(response.message || "Error al editar granelera");
      }

      return response;
    } catch (err: any) {
      error.value = err instanceof Error ? err.message : "Error al editar granelera";
      throw err;
    } finally {
      loading.value = false;
    }
  };

  const asignarPiloto = async (payload: { granelera_id: number; piloto_id: number }) => {
    try {
      loading.value = true;
      error.value = null;

      const body = {
        action: "asignar_piloto",
        ...payload,
      };

      const response = await useApiFetch<ApiResponse<any>>(
        "/supervisor/granelera",
        {
          method: "POST",
          body,
        }
      );

      if (!response.success) {
        throw new Error(response.message || "Error al asignar piloto");
      }

      return response;
    } catch (err: any) {
      error.value = err instanceof Error ? err.message : "Error al asignar piloto";
      throw err;
    } finally {
      loading.value = false;
    }
  };

  return {
    loading,
    error,
    obtenerGraneleras,
    crearGranelera,
    editarGranelera,
    asignarPiloto,
  };
};
