/**
 * CreateAppointmentModal Component
 *
 * Modal for creating new appointments with customer, service, and participant selection.
 * Supports both linked customers and participants with external email addresses.
 */

import React, { useState, useMemo, useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import { X, Search, User as UserIcon, Calendar, Clock, Users } from 'lucide-react';
import { createPortal } from 'react-dom';
import { Resource, Service, ParticipantInput } from '../types';
import { useCustomers } from '../hooks/useCustomers';
import { ParticipantSelector } from './ParticipantSelector';

interface CreateAppointmentModalProps {
  resources: Resource[];
  services: Service[];
  initialDate?: Date;
  initialResourceId?: string | null;
  onCreate: (appointmentData: {
    serviceId: string;
    customerId: string;
    startTime: Date;
    resourceId?: string | null;
    durationMinutes: number;
    notes?: string;
    participantsInput?: ParticipantInput[];
  }) => void;
  onClose: () => void;
  isCreating?: boolean;
}

export const CreateAppointmentModal: React.FC<CreateAppointmentModalProps> = ({
  resources,
  services,
  initialDate,
  initialResourceId,
  onCreate,
  onClose,
  isCreating = false,
}) => {
  const { t } = useTranslation();

  // Form state
  const [selectedServiceId, setSelectedServiceId] = useState('');
  const [selectedCustomerId, setSelectedCustomerId] = useState('');
  const [customerSearch, setCustomerSearch] = useState('');
  const [showCustomerDropdown, setShowCustomerDropdown] = useState(false);
  const [selectedDateTime, setSelectedDateTime] = useState(() => {
    // Default to initial date or now, rounded to nearest 15 min
    const date = initialDate || new Date();
    const minutes = Math.ceil(date.getMinutes() / 15) * 15;
    date.setMinutes(minutes, 0, 0);
    // Convert to datetime-local format
    const localDateTime = new Date(date.getTime() - date.getTimezoneOffset() * 60000)
      .toISOString()
      .slice(0, 16);
    return localDateTime;
  });
  const [selectedResourceId, setSelectedResourceId] = useState(initialResourceId || '');
  const [duration, setDuration] = useState(30);
  const [notes, setNotes] = useState('');
  const [participants, setParticipants] = useState<ParticipantInput[]>([]);

  // Fetch customers for search
  const { data: customers = [] } = useCustomers({ search: customerSearch });

  // Get selected customer details
  const selectedCustomer = useMemo(() => {
    return customers.find(c => c.id === selectedCustomerId);
  }, [customers, selectedCustomerId]);

  // Get selected service details
  const selectedService = useMemo(() => {
    return services.find(s => s.id === selectedServiceId);
  }, [services, selectedServiceId]);

  // When service changes, update duration to service default
  const handleServiceChange = useCallback((serviceId: string) => {
    setSelectedServiceId(serviceId);
    const service = services.find(s => s.id === serviceId);
    if (service) {
      setDuration(service.durationMinutes);
    }
  }, [services]);

  // Handle customer selection from search
  const handleSelectCustomer = useCallback((customerId: string, customerName: string) => {
    setSelectedCustomerId(customerId);
    setCustomerSearch(customerName);
    setShowCustomerDropdown(false);
  }, []);

  // Filter customers based on search
  const filteredCustomers = useMemo(() => {
    if (!customerSearch.trim()) return [];
    return customers.slice(0, 10);
  }, [customers, customerSearch]);

  // Validation
  const canCreate = useMemo(() => {
    return selectedServiceId && selectedCustomerId && selectedDateTime && duration >= 15;
  }, [selectedServiceId, selectedCustomerId, selectedDateTime, duration]);

  // Handle create
  const handleCreate = useCallback(() => {
    if (!canCreate) return;

    const startTime = new Date(selectedDateTime);

    onCreate({
      serviceId: selectedServiceId,
      customerId: selectedCustomerId,
      startTime,
      resourceId: selectedResourceId || null,
      durationMinutes: duration,
      notes: notes.trim() || undefined,
      participantsInput: participants.length > 0 ? participants : undefined,
    });
  }, [canCreate, selectedServiceId, selectedCustomerId, selectedDateTime, selectedResourceId, duration, notes, participants, onCreate]);

  const modalContent = (
    <div
      className="fixed inset-0 z-50 flex items-center justify-center p-4 bg-black/50 backdrop-blur-sm"
      onClick={onClose}
    >
      <div
        className="w-full max-w-lg bg-white dark:bg-gray-800 rounded-xl shadow-xl border border-gray-200 dark:border-gray-700 overflow-hidden max-h-[90vh] flex flex-col"
        onClick={e => e.stopPropagation()}
      >
        {/* Header */}
        <div className="flex items-center justify-between px-6 py-4 border-b border-gray-200 dark:border-gray-700 bg-gradient-to-r from-brand-50 to-brand-100 dark:from-brand-900/30 dark:to-brand-800/30">
          <div className="flex items-center gap-3">
            <div className="p-2 bg-brand-500 rounded-lg">
              <Calendar className="w-5 h-5 text-white" />
            </div>
            <h3 className="text-lg font-semibold text-gray-900 dark:text-white">
              {t('scheduler.newAppointment', 'New Appointment')}
            </h3>
          </div>
          <button
            onClick={onClose}
            className="p-1 text-gray-400 hover:bg-white/50 dark:hover:bg-gray-700/50 rounded-full transition-colors"
          >
            <X size={20} />
          </button>
        </div>

        {/* Scrollable content */}
        <div className="overflow-y-auto flex-1 p-6 space-y-5">
          {/* Service Selection */}
          <div>
            <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
              {t('services.title', 'Service')} <span className="text-red-500">*</span>
            </label>
            <select
              value={selectedServiceId}
              onChange={(e) => handleServiceChange(e.target.value)}
              className="w-full px-3 py-2.5 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg text-sm text-gray-900 dark:text-white focus:outline-none focus:ring-2 focus:ring-brand-500"
            >
              <option value="">{t('scheduler.selectService', 'Select a service...')}</option>
              {services.filter(s => s.is_active !== false).map(service => (
                <option key={service.id} value={service.id}>
                  {service.name} ({service.durationMinutes} min)
                </option>
              ))}
            </select>
          </div>

          {/* Customer Selection */}
          <div className="relative">
            <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
              {t('customers.title', 'Customer')} <span className="text-red-500">*</span>
            </label>
            <div className="relative">
              <Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-gray-400" />
              <input
                type="text"
                value={customerSearch}
                onChange={(e) => {
                  setCustomerSearch(e.target.value);
                  setShowCustomerDropdown(true);
                  if (!e.target.value.trim()) {
                    setSelectedCustomerId('');
                  }
                }}
                onFocus={() => setShowCustomerDropdown(true)}
                placeholder={t('customers.searchPlaceholder', 'Search customers by name or email...')}
                className="w-full pl-10 pr-4 py-2.5 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg text-sm text-gray-900 dark:text-white focus:outline-none focus:ring-2 focus:ring-brand-500"
              />
              {selectedCustomer && (
                <button
                  onClick={() => {
                    setSelectedCustomerId('');
                    setCustomerSearch('');
                  }}
                  className="absolute right-3 top-1/2 -translate-y-1/2 p-1 text-gray-400 hover:text-gray-600"
                >
                  <X size={14} />
                </button>
              )}
            </div>

            {/* Customer search results dropdown */}
            {showCustomerDropdown && customerSearch.trim() && (
              <div className="absolute z-50 w-full mt-1 bg-white dark:bg-gray-800 border dark:border-gray-700 rounded-lg shadow-lg max-h-60 overflow-y-auto">
                {filteredCustomers.length === 0 ? (
                  <div className="px-4 py-3 text-sm text-gray-500 dark:text-gray-400">
                    {t('common.noResults', 'No results found')}
                  </div>
                ) : (
                  filteredCustomers.map((customer) => (
                    <button
                      key={customer.id}
                      type="button"
                      onClick={() => handleSelectCustomer(customer.id, customer.name)}
                      className="w-full px-4 py-2 flex items-center gap-3 hover:bg-gray-100 dark:hover:bg-gray-700 text-left"
                    >
                      <div className="flex-shrink-0 w-8 h-8 bg-gray-200 dark:bg-gray-600 rounded-full flex items-center justify-center">
                        <UserIcon className="w-4 h-4 text-gray-500 dark:text-gray-400" />
                      </div>
                      <div className="flex-grow min-w-0">
                        <div className="text-sm font-medium text-gray-900 dark:text-white truncate">
                          {customer.name}
                        </div>
                        <div className="text-xs text-gray-500 dark:text-gray-400 truncate">
                          {customer.email}
                        </div>
                      </div>
                    </button>
                  ))
                )}
              </div>
            )}

            {/* Click outside to close dropdown */}
            {showCustomerDropdown && (
              <div
                className="fixed inset-0 z-40"
                onClick={() => setShowCustomerDropdown(false)}
              />
            )}
          </div>

          {/* Date, Time & Duration */}
          <div className="grid grid-cols-2 gap-4">
            <div>
              <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                {t('scheduler.selectDate', 'Date')} & {t('scheduler.selectTime', 'Time')} <span className="text-red-500">*</span>
              </label>
              <input
                type="datetime-local"
                value={selectedDateTime}
                onChange={(e) => setSelectedDateTime(e.target.value)}
                className="w-full px-3 py-2.5 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg text-sm text-gray-900 dark:text-white focus:outline-none focus:ring-2 focus:ring-brand-500"
              />
            </div>
            <div>
              <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                <Clock className="w-4 h-4 inline mr-1" />
                {t('scheduler.duration', 'Duration')} (min) <span className="text-red-500">*</span>
              </label>
              <input
                type="number"
                min="15"
                step="15"
                value={duration}
                onChange={(e) => {
                  const value = parseInt(e.target.value);
                  setDuration(value >= 15 ? value : 15);
                }}
                className="w-full px-3 py-2.5 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg text-sm text-gray-900 dark:text-white focus:outline-none focus:ring-2 focus:ring-brand-500"
              />
            </div>
          </div>

          {/* Resource Assignment */}
          <div>
            <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
              {t('scheduler.selectResource', 'Assign to Resource')}
            </label>
            <select
              value={selectedResourceId}
              onChange={(e) => setSelectedResourceId(e.target.value)}
              className="w-full px-3 py-2.5 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg text-sm text-gray-900 dark:text-white focus:outline-none focus:ring-2 focus:ring-brand-500"
            >
              <option value="">{t('scheduler.unassigned', 'Unassigned')}</option>
              {resources.map(resource => (
                <option key={resource.id} value={resource.id}>
                  {resource.name}
                </option>
              ))}
            </select>
          </div>

          {/* Participants Section */}
          <div className="p-4 bg-gray-50 dark:bg-gray-700/50 rounded-lg">
            <div className="flex items-center gap-2 mb-3">
              <Users className="w-4 h-4 text-gray-500" />
              <span className="text-sm font-medium text-gray-700 dark:text-gray-300">
                {t('participants.additionalParticipants', 'Additional Participants')}
              </span>
            </div>
            <ParticipantSelector
              value={participants}
              onChange={setParticipants}
              allowedRoles={['STAFF', 'CUSTOMER', 'OBSERVER']}
              allowExternalEmail={true}
            />
          </div>

          {/* Notes */}
          <div>
            <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
              {t('scheduler.notes', 'Notes')}
            </label>
            <textarea
              value={notes}
              onChange={(e) => setNotes(e.target.value)}
              rows={3}
              className="w-full px-3 py-2 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg text-sm text-gray-900 dark:text-white focus:outline-none focus:ring-2 focus:ring-brand-500 resize-none"
              placeholder={t('scheduler.notesPlaceholder', 'Add notes about this appointment...')}
            />
          </div>
        </div>

        {/* Footer with action buttons */}
        <div className="px-6 py-4 border-t border-gray-200 dark:border-gray-700 flex justify-end gap-3 bg-white dark:bg-gray-800">
          <button
            onClick={onClose}
            disabled={isCreating}
            className="px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-200 bg-gray-100 dark:bg-gray-700 rounded-lg hover:bg-gray-200 dark:hover:bg-gray-600 transition-colors disabled:opacity-50"
          >
            {t('common.cancel', 'Cancel')}
          </button>
          <button
            onClick={handleCreate}
            disabled={!canCreate || isCreating}
            className="px-4 py-2 text-sm font-medium text-white bg-brand-600 rounded-lg hover:bg-brand-700 transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
          >
            {isCreating ? t('common.creating', 'Creating...') : t('scheduler.createAppointment', 'Create Appointment')}
          </button>
        </div>
      </div>
    </div>
  );

  return createPortal(modalContent, document.body);
};

export default CreateAppointmentModal;
