import React, { useState, useEffect, useMemo } from 'react';
import { useNavigate, useSearchParams } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { FaFilter, FaChevronDown, FaChevronUp, FaTimes, FaCalendarAlt, FaMap } from 'react-icons/fa';
import MaintenanceCalendar from '../components/MaintenanceCalendar';
import LinkField from '../components/LinkField';

const MaintenanceCalendarPage: React.FC = () => {
  const { t } = useTranslation();
  const navigate = useNavigate();
  const [searchParams] = useSearchParams();
  
  // Filter states
  const [filterCompany, setFilterCompany] = useState('');
  const [filterDepartment, setFilterDepartment] = useState('');
  const [filterStatus, setFilterStatus] = useState('');
  const [filterAssignTo, setFilterAssignTo] = useState('');

  // Load filters from URL on mount
  useEffect(() => {
    const hospital = searchParams.get('hospital');
    const status = searchParams.get('status');
    
    if (hospital) setFilterCompany(hospital);
    if (status) setFilterStatus(status);
  }, [searchParams]);
  
  // View type states
  const [viewType, setViewType] = useState<'maintenance-log' | 'ppm-planner'>('maintenance-log');
  
  // UI states
  const [isFilterExpanded, setIsFilterExpanded] = useState(false);
  const [activeFilterCount, setActiveFilterCount] = useState(0);

  // Update active filter count
  useEffect(() => {
    const count = [filterCompany, filterDepartment, filterStatus, filterAssignTo].filter(Boolean).length;
    setActiveFilterCount(count);
  }, [filterCompany, filterDepartment, filterStatus, filterAssignTo]);

  const handleClearFilters = () => {
    setFilterCompany('');
    setFilterDepartment('');
    setFilterStatus('');
    setFilterAssignTo('');
  };

  const hasActiveFilters = filterCompany || filterDepartment || filterStatus || filterAssignTo;

  // Build filters for calendar - memoized to prevent object reference changes
  const calendarFilters: Record<string, any> = useMemo(() => {
    const filters: Record<string, any> = {};
    if (filterCompany) filters['company'] = filterCompany;
    if (filterDepartment) filters['department'] = filterDepartment;
    if (filterStatus) filters['maintenance_status'] = filterStatus;
    if (filterAssignTo) filters['assign_to_name'] = filterAssignTo;
    return filters;
  }, [filterCompany, filterDepartment, filterStatus, filterAssignTo]);

  return (
    <div className="flex flex-col h-screen bg-gray-50 dark:bg-gray-900 overflow-hidden">
      {/* Header - Single Row on Desktop/Laptop */}
      <div className="flex-shrink-0 bg-white dark:bg-gray-800 border-b border-gray-200 dark:border-gray-700 px-4 py-2.5 lg:px-6">
        <div className="flex flex-col md:flex-row md:items-center md:justify-between gap-3 md:gap-4">
          {/* Left Side - Title */}
          <div className="flex items-center gap-2.5 min-w-0">
            <FaCalendarAlt className="text-blue-600 dark:text-blue-400 flex-shrink-0" size={22} />
            <div className="min-w-0">
              <h1 className="text-lg md:text-xl font-bold text-gray-800 dark:text-white whitespace-nowrap">
                {t('maintenanceCalendarPage.title')}
              </h1>
            </div>
          </div>

          {/* Right Side - All Controls in Single Row */}
          <div className="flex items-end gap-2 md:gap-3 flex-wrap md:flex-nowrap">
            {/* View Type Dropdown */}
            <div className="relative">
              <label className="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1">
                {t('maintenanceCalendarPage.viewType')}
              </label>
              <select
                value={viewType}
                onChange={(e) => setViewType(e.target.value as 'maintenance-log' | 'ppm-planner')}
                className="px-2.5 md:px-3 py-1.5 text-xs md:text-sm border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 bg-white dark:bg-gray-700 text-gray-900 dark:text-white"
              >
                <option value="maintenance-log">{t('maintenanceCalendarPage.maintenanceLog')}</option>
                <option value="ppm-planner">{t('maintenanceCalendarPage.ppmPlanner')}</option>
              </select>
            </div>
            
            {/* Filters Button */}
            <div className="relative">
              <label className="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1 invisible">
                {t('maintenanceCalendarPage.filters')}
              </label>
              <button
                onClick={() => setIsFilterExpanded(!isFilterExpanded)}
                className={`px-3 md:px-4 py-1.5 md:py-2 border rounded-lg transition-colors flex items-center gap-1.5 md:gap-2 text-xs md:text-sm ${
                  hasActiveFilters
                    ? 'border-blue-500 bg-blue-50 dark:bg-blue-900/20 text-blue-600 dark:text-blue-400'
                    : 'border-gray-300 dark:border-gray-600 hover:bg-gray-50 dark:hover:bg-gray-700 text-gray-700 dark:text-gray-300'
                }`}
              >
                <FaFilter size={14} />
                <span className="hidden sm:inline">{t('maintenanceCalendarPage.filters')}</span>
                {activeFilterCount > 0 && (
                  <span className="bg-blue-600 text-white rounded-full w-4 h-4 md:w-5 md:h-5 flex items-center justify-center text-[10px] md:text-xs">
                    {activeFilterCount}
                  </span>
                )}
                {isFilterExpanded ? <FaChevronUp size={12} /> : <FaChevronDown size={12} />}
              </button>
            </div>
            
            {/* Yearly PPM Planner Button */}
            {viewType === 'ppm-planner' && (
              <div className="relative">
                <label className="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1 invisible">
                  {t('maintenanceCalendarPage.yearlyMap')}
                </label>
                <button
                  onClick={() => navigate('/yearly-ppm-planner')}
                  className="px-3 md:px-4 py-1.5 md:py-2 bg-purple-600 hover:bg-purple-700 text-white rounded-lg transition-colors flex items-center gap-1.5 md:gap-2 text-xs md:text-sm font-medium whitespace-nowrap"
                  title={t('maintenanceCalendarPage.yearlyMapTitle')}
                >
                  <FaMap size={14} />
                  <span className="hidden sm:inline">{t('maintenanceCalendarPage.yearlyMap')}</span>
                  <span className="sm:hidden">{t('maintenanceCalendarPage.mapShort')}</span>
                </button>
              </div>
            )}
          </div>
        </div>

        {/* Filter Panel */}
        {isFilterExpanded && (
          <div className="mt-2.5 md:mt-3 p-3 md:p-4 bg-gray-50 dark:bg-gray-700/50 rounded-lg border border-gray-200 dark:border-gray-600">
            <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-3">
              {/* Hospital */}
              <div className="relative z-[60]">
                <LinkField
                  label={t('maintenanceCalendarPage.hospital')}
                  doctype="Company"
                  value={filterCompany}
                  onChange={(val) => setFilterCompany(val)}
                  placeholder={t('maintenanceCalendarPage.selectHospital')}
                  compact={true}
                />
                {filterCompany && (
                  <button
                    onClick={() => setFilterCompany('')}
                    className="absolute right-2 top-6 text-gray-400 hover:text-red-500 transition-colors z-10"
                  >
                    <FaTimes size={10} />
                  </button>
                )}
              </div>

              {/* Department */}
              <div className="relative z-[55]">
                <LinkField
                  label={t('maintenanceCalendarPage.department')}
                  doctype="Department"
                  value={filterDepartment}
                  onChange={(val) => setFilterDepartment(val)}
                  placeholder={t('maintenanceCalendarPage.allDepartments')}
                  compact={true}
                />
                {filterDepartment && (
                  <button
                    onClick={() => setFilterDepartment('')}
                    className="absolute right-2 top-6 text-gray-400 hover:text-red-500 transition-colors z-10"
                  >
                    <FaTimes size={10} />
                  </button>
                )}
              </div>

              {/* Status */}
              <div className="relative">
                <label className="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1">
                  {t('maintenanceCalendarPage.status')}
                </label>
                <select
                  value={filterStatus}
                  onChange={(e) => setFilterStatus(e.target.value)}
                  className="w-full px-3 py-1.5 text-sm border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 bg-white dark:bg-gray-700 text-gray-900 dark:text-white"
                >
                  <option value="">{t('maintenanceCalendarPage.allStatuses')}</option>
                  <option value="Planned">{t('maintenanceCalendarPage.planned')}</option>
                  <option value="Completed">{t('maintenanceCalendarPage.completed')}</option>
                  <option value="Overdue">{t('maintenanceCalendarPage.overdue')}</option>
                  <option value="Cancelled">{t('maintenanceCalendarPage.cancelled')}</option>
                </select>
                {filterStatus && (
                  <button
                    onClick={() => setFilterStatus('')}
                    className="absolute right-8 top-7 text-gray-400 hover:text-red-500 transition-colors"
                  >
                    <FaTimes size={10} />
                  </button>
                )}
              </div>

              {/* Assigned To */}
              <div className="relative z-[50]">
                <LinkField
                  label={t('maintenanceCalendarPage.assignedTo')}
                  doctype="User"
                  value={filterAssignTo}
                  onChange={(val) => setFilterAssignTo(val)}
                  placeholder={t('maintenanceCalendarPage.allTechnicians')}
                  compact={true}
                />
                {filterAssignTo && (
                  <button
                    onClick={() => setFilterAssignTo('')}
                    className="absolute right-2 top-6 text-gray-400 hover:text-red-500 transition-colors z-10"
                  >
                    <FaTimes size={10} />
                  </button>
                )}
              </div>
            </div>
            {hasActiveFilters && (
              <div className="mt-3 flex justify-end">
                <button
                  onClick={handleClearFilters}
                  className="px-3 py-1.5 text-sm text-gray-600 dark:text-gray-400 hover:text-gray-800 dark:hover:text-gray-200 flex items-center gap-2"
                >
                  <FaTimes />
                  {t('maintenanceCalendarPage.clearFilters')}
                </button>
              </div>
            )}
          </div>
        )}
      </div>

      {/* Calendar */}
      <div className="flex-1 overflow-hidden px-3 pb-3 lg:px-4 lg:pb-4">
        <MaintenanceCalendar 
          filters={calendarFilters} 
          viewType={viewType}
          timeView="day-month"
        />
      </div>
    </div>
  );
};

export default MaintenanceCalendarPage;
