import { baseAPI } from "@/Shared/API/baseAPI";
import { IFetch } from "@/Shared/types";
import axios from "axios";
import { create } from "zustand";

interface IStatistic {
  name: string;
  broken_road_1: number;
  hotbed_of_accidents_2: number;
  local_defect_3: number;
  repair_plans_4: number;
  repaired_5: number;
  local_defect_fixed_6: number;
}

interface IStatisticStore extends IFetch {
  data: IStatistic[];
  getStatistics: (location?: string) => Promise<void>;
}

export const useStatistics = create<IStatisticStore>((set) => ({
  data: [],
  loading: false,
  error: "",
  getStatistics: async (location: string = "village") => {
    try {
      set({ loading: true });

      const response = await axios.get<IStatistic[]>(
        `${baseAPI}/report/${location}/stats/`
      );

      set({ data: response.data });
    } catch (error: any) {
      set({ error: error.message });
    } finally {
      set({ loading: false });
    }
  },
}));
