import { useQuery } from '@tanstack/react-query';

import { UNIT_INFO_MAIN_ROLES } from '../constants/custom';
import api from '../services/api';
import { useUnitAdvertisingAccess } from './useUnitAdvertising';
import { useUnitMembersAccess } from './useUnitMembership';
import { useUnitRegistrationAccess } from './useUnitRegistration';

export const useUnit = (token: string, unitId: string) => {
  const { data: details, isLoading: isDetailLoading } = useUnitDetail(token, unitId);
  const { data: parentDetails, isLoading: isParentDetailLoading } = useUnitDetail(
    token,
    details?.parentUnitId ?? '',
  );
  const { data: tree, isLoading: isTreeLoading } = useUnitTree(token, unitId);
  const { data: contacts, isLoading: isContactsLoading } = useUnitContacts(token, unitId);
  const { data: functions, isLoading: isFunctionsLoading } = useUnitFunctions(token, unitId);

  const unit = {
    ...details,
    addressExists: !!details?.city || !!details?.street || !!details?.postcode,
    postalAddressExists:
      !!details?.postalCity || !!details?.postalStreet || !!details?.postalPostcode,
    parentUnitName: parentDetails?.displayName ?? '',
    unitTree: tree ?? [],
    contacts: contacts ?? [],
    functions: functions ?? [],
    mainFunctions: functions?.filter(f => UNIT_INFO_MAIN_ROLES.includes(Number(f.functionTypeId))),
  };

  const isLoading =
    isDetailLoading ||
    isTreeLoading ||
    isContactsLoading ||
    isFunctionsLoading ||
    (details?.parentUnitId ? isParentDetailLoading : false);

  return { unit, isLoading };
};

export const useUnitDetail = (token: string, unitId: string) => {
  return useQuery(
    ['unit-detail', [token, unitId]],
    () => api.organizationUnit.unitDetailGet(token, unitId),
    {
      enabled: !!token && !!unitId,
    },
  );
};

export const useUnitTree = (token: string, unitId: string) => {
  return useQuery(
    ['unit-tree-all', [token, unitId]],
    () => api.organizationUnit.unitTreeAllGet(token, unitId),
    {
      enabled: !!token && !!unitId,
    },
  );
};

export const useUnitContacts = (token: string, unitId: string) => {
  return useQuery(
    ['unit-contact-all', [token, unitId]],
    () => api.organizationUnit.unitContactAllGet(token, unitId),
    {
      enabled: !!token && !!unitId,
    },
  );
};

export const useUnitFunctions = (token: string, unitId: string) => {
  return useQuery(
    ['unit-functions-all', [token, unitId]],
    () => api.organizationUnit.unitFunctionAllGet(token, unitId),
    {
      enabled: !!token && !!unitId,
    },
  );
};

export const useUnitAccess = (token: string, unitId: string) => {
  const { hasAccess: hasMembersAccess, isLoading: isMembersLoading } = useUnitMembersAccess(
    token,
    unitId,
  );
  const { hasAccess: hasRegistrationAccess, isLoading: isRegistrationLoading } =
    useUnitRegistrationAccess(token, unitId);

  const { hasAccess: hasAdvertisingAccess, isLoading: isAdvertisingLoading } =
    useUnitAdvertisingAccess(token, unitId);

  const isLoading = isMembersLoading || isRegistrationLoading || isAdvertisingLoading;
  return { hasMembersAccess, hasRegistrationAccess, hasAdvertisingAccess, isLoading };
};
