import React, { useState, useEffect, useRef } from 'react';
import { 
  useSearchParams, 
  useNavigate, 
  Link 
} from 'react-router-dom';
import { 
  Search, 
  Calendar, 
  AlertCircle, 
  ArrowLeft,
  Home,
  Users,
} from 'lucide-react';
import RoomCard from '../../features/rooms/components/RoomCard';
import RoomCardSkeleton from '../../features/rooms/components/RoomCardSkeleton';
import Pagination from '../../shared/components/Pagination';
import { searchAvailableRooms } from '../../features/rooms/services/roomService';
import type { Room } from '../../features/rooms/services/roomService';
import { toast } from 'react-toastify';
import { parseDateLocal } from '../../shared/utils/format';
import { logger } from '../../shared/utils/logger';

const SearchResultsPage: React.FC = () => {
  const [searchParams] = useSearchParams();
  const navigate = useNavigate();
  
  const [rooms, setRooms] = useState<Room[]>([]);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);
  const [pagination, setPagination] = useState({
    total: 0,
    page: 1,
    limit: 12,
    totalPages: 1,
  });
  const abortControllerRef = useRef<AbortController | null>(null);

  
  const from = searchParams.get('from') || '';
  const to = searchParams.get('to') || '';
  const type = searchParams.get('type') || '';
  const capacityParam = searchParams.get('capacity') || '';
  const capacity = capacityParam ? Number(capacityParam) : undefined;
  const pageParam = searchParams.get('page') || '';
  const page = pageParam ? Number(pageParam) : 1;

  useEffect(() => {
    
    if (!from || !to) {
      toast.error(
        'Missing search information. ' +
        'Please select check-in and check-out dates.'
      );
      navigate('/');
      return;
    }

    // Cancel previous request if exists
    if (abortControllerRef.current) {
      abortControllerRef.current.abort();
    }

    // Create new abort controller
    abortControllerRef.current = new AbortController();

    fetchAvailableRooms();

    // Cleanup: abort request on unmount
    return () => {
      if (abortControllerRef.current) {
        abortControllerRef.current.abort();
      }
    };
  // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [from, to, type, capacity, page, navigate]);

  const fetchAvailableRooms = async () => {
    try {
      setLoading(true);
      setError(null);

      const response = await searchAvailableRooms({
        from,
        to,
        type: type || undefined,
        capacity: capacity || undefined,
        page,
        limit: 12,
      });

      if (
        response.success || 
        response.status === 'success'
      ) {
        setRooms(response.data.rooms || []);
        if (response.data.pagination) {
          setPagination(response.data.pagination);
        } else {
          
          const total = response.data.rooms
            ? response.data.rooms.length
            : 0;
          const limit = 12;
          setPagination({
            total,
            page,
            limit,
            totalPages: Math.max(1, Math.ceil(total / limit)),
          });
        }
      } else {
        throw new Error('Unable to search rooms');
      }
    } catch (err: unknown) {
      // Don't show error if request was aborted
      if (err && typeof err === 'object' && 'name' in err && err.name === 'AbortError') {
        return;
      }
      logger.error('Error searching rooms', err);
      const message =
        (err && typeof err === 'object' && 'response' in err && err.response && typeof err.response === 'object' && 'data' in err.response && err.response.data && typeof err.response.data === 'object' && 'message' in err.response.data && typeof err.response.data.message === 'string' ? err.response.data.message : undefined) ||
        'Unable to search available rooms';
      setError(message);
      toast.error(message);
    } finally {
      setLoading(false);
    }
  };

  const formatDate = (dateString: string) => {
    const date = parseDateLocal(dateString);
    return date.toLocaleDateString('en-US', {
      day: '2-digit',
      month: '2-digit',
      year: 'numeric',
    });
  };

  return (
    <div className="min-h-screen bg-gray-50">
      <div className="max-w-7xl mx-auto px-4 py-8">
        {}
        <Link
          to="/"
          className="inline-flex items-center gap-2 bg-indigo-600 
            text-white px-3 py-2 rounded-md hover:bg-indigo-700 
            disabled:bg-gray-400 mb-6 transition-colors"
        >
          <ArrowLeft className="w-5 h-5" />
          <span>Back to home</span>
        </Link>

        {}
        <div 
          className="bg-white rounded-lg shadow-sm 
            p-6 mb-8"
        >
          <div className="flex items-start justify-between 
            flex-wrap gap-4"
          >
            <div>
              <h1 
                className="text-3xl font-bold 
                  text-gray-900 mb-4"
              >
                Search Results
              </h1>
              
              <div 
                className="flex flex-wrap items-center 
                  gap-4 text-gray-700"
              >
                <div 
                  className="flex items-center gap-2"
                >
                  <Calendar className="w-5 h-5 
                    text-indigo-600" 
                  />
                  <span>
                    <strong>Check-in:</strong>{' '}
                    {formatDate(from)}
                  </span>
                </div>
                
                <div 
                  className="flex items-center gap-2"
                >
                  <Calendar className="w-5 h-5 
                    text-indigo-600" 
                  />
                  <span>
                    <strong>Check-out:</strong>{' '}
                    {formatDate(to)}
                  </span>
                </div>

                {type && (
                  <div 
                    className="flex items-center gap-2"
                  >
                    <Home className="w-5 h-5 
                      text-indigo-600" 
                    />
                    <span>
                      <strong>Room Type:</strong>{' '}
                      {type}
                    </span>
                  </div>
                )}
                {capacity && (
                  <div className="flex items-center gap-2">
                    <Users className="w-5 h-5 text-indigo-600" />
                    <span>
                      <strong>Guests:</strong>{' '}
                      {capacity}
                    </span>
                  </div>
                )}
              </div>
            </div>

            <button
              onClick={() => navigate('/')}
              className="px-4 py-2 border border-gray-300 
                bg-indigo-600 text-white rounded-lg 
                hover:bg-indigo-700 disabled:bg-gray-400 
                transition-colors flex items-center gap-2"
            >
              <Search className="w-4 h-4" />
              New Search
            </button>
          </div>
        </div>

        {}
        {loading && (
          <div>
            <p 
              className="text-gray-600 mb-6 
                text-center animate-pulse"
            >
              Searching for available rooms...
            </p>
            <div 
              className="grid grid-cols-1 md:grid-cols-2 
                lg:grid-cols-3 gap-6"
            >
              {[...Array(6)].map((_, index) => (
                <RoomCardSkeleton key={index} />
              ))}
            </div>
          </div>
        )}

        {}
        {error && !loading && (
          <div 
            className="bg-red-50 border border-red-200 
              rounded-lg p-8 text-center"
          >
            <AlertCircle 
              className="w-12 h-12 text-red-500 
                mx-auto mb-3" 
            />
            <p className="text-red-700 font-medium mb-4">
              {error}
            </p>
            <button
              onClick={fetchAvailableRooms}
              className="px-6 py-2 bg-red-600 
                text-white rounded-lg 
                hover:bg-red-700 transition-colors"
            >
              Try Again
            </button>
          </div>
        )}

        {}
        {!loading && !error && (
          <>
            {rooms.length > 0 ? (
              <>
                <div 
                  className="flex items-center 
                    justify-between mb-6"
                >
                </div>

                <div 
                  className="grid grid-cols-1 
                    md:grid-cols-2 lg:grid-cols-3 
                    gap-6"
                >
                  {rooms.map((room) => (
                    <RoomCard key={room.id} room={room} />
                  ))}
                </div>

                <Pagination
                  currentPage={pagination.page}
                  totalPages={pagination.totalPages}
                />
              </>
            ) : (
              
              <div 
                className="bg-white rounded-lg 
                  shadow-sm p-12 text-center"
              >
                <div 
                  className="w-24 h-24 bg-gray-100 
                    rounded-full flex items-center 
                    justify-center mx-auto mb-6"
                >
                  <Search 
                    className="w-12 h-12 text-gray-400" 
                  />
                </div>
                
                <h3 
                  className="text-2xl font-bold 
                    text-gray-900 mb-3"
                >
                  No matching rooms found
                </h3>
                
                <p 
                  className="text-gray-600 mb-6 
                    max-w-md mx-auto"
                >
                  Sorry, there are no available rooms 
                  for the selected dates. 
                  Please try searching with different dates 
                  or room types.
                </p>

                <div 
                  className="flex flex-col sm:flex-row 
                    gap-3 justify-center"
                >
                  <button
                    onClick={() => navigate('/')}
                    className="px-6 py-3 bg-indigo-600 
                      text-white rounded-lg 
                      hover:bg-indigo-700 
                      transition-colors font-semibold
                      inline-flex items-center 
                      justify-center gap-2"
                  >
                    <Search className="w-5 h-5" />
                    Search Again
                  </button>
                  
                  <Link
                    to="/rooms"
                    className="px-6 py-3 border 
                      border-gray-300 text-gray-700 
                      rounded-lg hover:bg-gray-50 
                      transition-colors font-semibold
                      inline-flex items-center 
                      justify-center gap-2"
                  >
                    View All Rooms
                  </Link>
                </div>
              </div>
            )}
          </>
        )}
      </div>
    </div>
  );
};

export default SearchResultsPage;
