import { useMemo, useState } from 'react';

import { useContentCatalogPayload } from '@/business/content-catalog/hooks';
import { canPersistPersonalScopeResults } from '@/business/scope/selectors';
import { useTodayZoneCheckIn } from '@/business/zone-checkin/hooks';
import { canZoneCheckIn } from '@/business/zone-checkin/selectors';
import {
  getSelectedZone,
  getZonesSafetyConnection,
} from '@/business/zones/selectors';
import type { ZonesOfRegulationPage } from '@/business/zones/types';
import { useScopeContext } from '@/shared/app/scope-context';
import { CONTENT_CATALOG_TYPES } from '@/shared/constants/contentCatalog';
import {
  DEFAULT_EXPANDED_ZONE,
  DEFAULT_ZONES_TAB,
} from '@/shared/constants/zonesOfRegulation';
import type { ZonesOfRegulationTab } from '@/shared/constants/zonesOfRegulation';
import type {
  CurrentUser,
} from '@/shared/types/auth';
import type {
  ZoneColor,
  ZoneInfo,
  ZonesOfRegulationPageContent,
} from '@/shared/types/app';
import { getOptionalErrorMessage } from '@/shared/errors/errorMessages';

const EMPTY_ZONES_PAGE_CONTENT: ZonesOfRegulationPageContent = {
  safetyConnections: [],
};

interface UseZonesOfRegulationPageOptions {
  readonly user: CurrentUser | null | undefined;
}

export function useZonesOfRegulationPage(options: UseZonesOfRegulationPageOptions): ZonesOfRegulationPage {
  const { user } = options;
  const { ownTenant, selectedTenant } = useScopeContext();
  const zonesQuery = useContentCatalogPayload<readonly ZoneInfo[]>(
    CONTENT_CATALOG_TYPES.regulationZones,
    [],
  );
  const pageContentQuery = useContentCatalogPayload<ZonesOfRegulationPageContent>(
    CONTENT_CATALOG_TYPES.zonesOfRegulationPageContent,
    EMPTY_ZONES_PAGE_CONTENT,
  );
  const [expandedZone, setExpandedZone] = useState<ZoneColor | null>(DEFAULT_EXPANDED_ZONE);
  const [activeTab, setActiveTab] = useState<ZonesOfRegulationTab>(DEFAULT_ZONES_TAB);
  const canUseZoneCheckIn = canPersistPersonalScopeResults(ownTenant, selectedTenant) && canZoneCheckIn(user);
  const zoneCheckIn = useTodayZoneCheckIn({ enabled: canUseZoneCheckIn });
  const zones = zonesQuery.payload;
  const pageContent = pageContentQuery.payload;
  const checkedInZone = zoneCheckIn.todayZone;
  const selectedZone = useMemo(
    () => getSelectedZone(zones, expandedZone ?? checkedInZone),
    [checkedInZone, expandedZone, zones],
  );
  const safetyConnection = useMemo(
    () => getZonesSafetyConnection(pageContent.safetyConnections, selectedZone?.color ?? null),
    [pageContent.safetyConnections, selectedZone?.color],
  );

  async function selectZone(zone: ZoneColor) {
    setExpandedZone(zone);
    if (canUseZoneCheckIn) {
      await zoneCheckIn.setZone(zone);
    }
  }

  return {
    zones,
    pageContent,
    expandedZone,
    checkedInZone,
    selectedZone,
    activeTab,
    safetyConnection,
    isLoading: zonesQuery.isLoading || pageContentQuery.isLoading,
    isZoneSaving: zoneCheckIn.isSaving,
    zonesError: zonesQuery.error,
    pageContentError: pageContentQuery.error,
    zoneCheckInErrorMessage: getOptionalErrorMessage(zoneCheckIn.error),
    setActiveTab,
    selectZone,
  };
}
