// FiscalYearSelector - Dropdown for selecting active fiscal year (regnskabsar)

import { useState, useEffect, useRef } from 'react';
import { Select, Space, Typography, Tag, Divider, Button, Skeleton } from 'antd';
import {
  CalendarOutlined,
  PlusOutlined,
  SettingOutlined,
  CheckCircleOutlined,
  MinusCircleOutlined,
  LockOutlined,
} from '@ant-design/icons';
import { usePeriodStore } from '@/stores/periodStore';
import { useCompanyStore } from '@/stores/companyStore';
import { useFiscalYears } from '@/api/queries/fiscalYearQueries';
import type { FiscalYear } from '@/types/periods';
import { formatDateShort } from '@/lib/formatters';
import CreateFiscalYearModal from '@/components/modals/CreateFiscalYearModal';

const { Text } = Typography;

/**
 * Status badge configuration
 */
const STATUS_CONFIG: Record<FiscalYear['status'], {
  color: string;
  icon: React.ReactNode;
  label: string;
}> = {
  open: {
    color: 'success',
    icon: <CheckCircleOutlined />,
    label: 'Åben',
  },
  closed: {
    color: 'warning',
    icon: <MinusCircleOutlined />,
    label: 'Lukket',
  },
  locked: {
    color: 'error',
    icon: <LockOutlined />,
    label: 'Låst',
  },
};

interface FiscalYearSelectorProps {
  onCreateNew?: () => void;
  onManage?: () => void;
}

export default function FiscalYearSelector({ onCreateNew, onManage }: FiscalYearSelectorProps) {
  const { activeCompany } = useCompanyStore();
  const {
    fiscalYears,
    currentFiscalYear,
    setFiscalYears,
    setCurrentFiscalYear,
  } = usePeriodStore();

  const { data: fiscalYearsData = [], isLoading } = useFiscalYears(activeCompany?.id);

  const [createModalOpen, setCreateModalOpen] = useState(false);

  // Track company changes
  const prevCompanyIdRef = useRef<string | undefined>(activeCompany?.id);

  // Reset fiscal year state when company changes
  useEffect(() => {
    if (activeCompany?.id !== prevCompanyIdRef.current) {
      const isInitialMount = prevCompanyIdRef.current === undefined;
      prevCompanyIdRef.current = activeCompany?.id;

      if (!isInitialMount) {
        // Company changed - reset fiscal year state
        setFiscalYears([]);
        setCurrentFiscalYear(null);
      }
    }
  }, [activeCompany?.id, setFiscalYears, setCurrentFiscalYear]);

  // Sync fiscal years from API to store
  useEffect(() => {
    if (fiscalYearsData.length > 0) {
      setFiscalYears(fiscalYearsData);

      // Get current value without adding to dependencies to avoid infinite loop
      const current = usePeriodStore.getState().currentFiscalYear;

      // Validate currentFiscalYear belongs to this company's data
      const isValid = current &&
        fiscalYearsData.some(fy => fy.id === current.id);

      if (!isValid) {
        const openYear = fiscalYearsData.find(y => y.status === 'open');
        setCurrentFiscalYear(openYear || fiscalYearsData[0]);
      }
    }
  }, [fiscalYearsData, setFiscalYears, setCurrentFiscalYear]);

  const handleFiscalYearChange = (yearId: string) => {
    const year = fiscalYears.find((y) => y.id === yearId);
    if (year) {
      setCurrentFiscalYear(year);
    }
  };

  const handleCreateNew = () => {
    if (onCreateNew) {
      onCreateNew();
    } else {
      setCreateModalOpen(true);
    }
  };

  const handleCloseCreateModal = () => {
    setCreateModalOpen(false);
  };

  const handleCreateSuccess = (newYear: FiscalYear) => {
    setCurrentFiscalYear(newYear);
    setCreateModalOpen(false);
  };

  const handleManage = () => {
    if (onManage) {
      onManage();
    } else {
      // Navigate to settings page
      console.log('Navigate to fiscal year settings');
    }
  };

  if (isLoading) {
    return <Skeleton.Input style={{ width: 200 }} active />;
  }

  // Sort fiscal years by start date descending (newest first)
  const sortedYears = [...fiscalYears].sort(
    (a, b) => new Date(b.startDate).getTime() - new Date(a.startDate).getTime()
  );

  return (
    <Space>
      <CalendarOutlined style={{ fontSize: 16, color: '#8c8c8c' }} />
      <Select
        value={currentFiscalYear?.id}
        onChange={handleFiscalYearChange}
        style={{ minWidth: 200 }}
        optionLabelProp="label"
        popupMatchSelectWidth={false}
        popupRender={(menu) => (
          <>
            {menu}
            <Divider style={{ margin: '8px 0' }} />
            <Space style={{ padding: '0 8px 8px' }}>
              <Button
                type="text"
                icon={<PlusOutlined />}
                onClick={handleCreateNew}
                size="small"
              >
                Opret nyt regnskabsår
              </Button>
              <Button
                type="text"
                icon={<SettingOutlined />}
                onClick={handleManage}
                size="small"
              >
                Administrer
              </Button>
            </Space>
          </>
        )}
        options={sortedYears.map((year) => ({
          value: year.id,
          label: `Regnskabsår ${year.name}`,
          year,
        }))}
        optionRender={(option) => {
          const year = option.data.year;
          // Type guard - ensure year exists and has required properties
          if (!year || typeof year !== 'object' || !('status' in year)) {
            return null;
          }
          const fiscalYear = year as FiscalYear;
          const statusConfig = STATUS_CONFIG[fiscalYear.status];

          return (
            <Space
              direction="vertical"
              size={0}
              style={{ padding: '4px 0', width: '100%' }}
            >
              <Space style={{ width: '100%', justifyContent: 'space-between' }}>
                <Text strong>{fiscalYear.name}</Text>
                <Tag
                  color={statusConfig.color}
                  icon={statusConfig.icon}
                  style={{ marginLeft: 8 }}
                >
                  {statusConfig.label}
                </Tag>
              </Space>
              <Text type="secondary" style={{ fontSize: 12 }}>
                {formatDateShort(fiscalYear.startDate)} - {formatDateShort(fiscalYear.endDate)}
              </Text>
            </Space>
          );
        }}
      />
      {currentFiscalYear && (
        <Tag
          color={STATUS_CONFIG[currentFiscalYear.status].color}
          icon={STATUS_CONFIG[currentFiscalYear.status].icon}
        >
          {STATUS_CONFIG[currentFiscalYear.status].label}
        </Tag>
      )}

      {/* Create Fiscal Year Modal */}
      <CreateFiscalYearModal
        open={createModalOpen}
        onClose={handleCloseCreateModal}
        onSuccess={handleCreateSuccess}
      />
    </Space>
  );
}
