import { ref, onMounted, computed } from 'vue';
import {
  CompanyInfoItem,
  getAuthSceneList,
  getAlgoByCameraId,
  ViolationsQueryParam,
  ViolationCount,
  getViolation,
} from '@/api/home/home.ts';
import dayjs from 'dayjs';
import { useUserStore } from '@/store/modules/user.ts';
import { storeToRefs } from 'pinia';

export function useHomeInfo() {
  const userStore = useUserStore();
  const { info } = storeToRefs(userStore);

  const userName = computed(() => info.value.username);

  const sceneData = ref<CompanyInfoItem[]>([]);
  const violationData = ref<ViolationCount>({} as ViolationCount);

  const getSceneData = () => {
    getAuthSceneList().then((res) => {
      sceneData.value = res;
    });
  };

  const getAlgoList = (cameraId: number) => {
    return getAlgoByCameraId({ cameraId }).then((res) => {
      return res;
    });
  };

  const getViolationCount = (range: string[]) => {
    const params: ViolationsQueryParam = {
      startDate: range[0],
      endDate: range[1],
      userName: userName.value,
    };
    getViolation(params).then((res) => {
      violationData.value = res;
    });
  };

  const formatDay = (day) => {
    return dayjs(day).format('YYYY-MM-DD');
  };

  onMounted(() => {
    getSceneData();

    const today = formatDay(dayjs());
    getViolationCount([today, today]);
  });

  return {
    sceneData,
    violationData,
    getSceneData,
    getAlgoList,
    getViolationCount,
  };
}

export default useHomeInfo;
