'use client';

import { createContext, useContext, useState, useEffect, useCallback } from 'react';
import { getUserFavoriteProperties, addFavoriteProperty, removeFavoriteProperty } from '../utils/api';
import AuthService from '../services/AuthService';

const FavoritesContext = createContext();

export const useFavorites = () => {
  const context = useContext(FavoritesContext);
  if (!context) {
    throw new Error('useFavorites must be used within FavoritesProvider');
  }
  return context;
};

function mapApiFavorite(item) {
  const info = item.propertyInformation || {};
  let details = {};
  if (typeof info.detailsJSON === 'object' && info.detailsJSON) {
    details = info.detailsJSON;
  } else {
    try { details = JSON.parse(info.detailsJSON || '{}'); } catch {}
  }

  const price = item.monthlyRent || item.dailyRent || 0;
  const priceUnit = item.monthlyRent ? 'monthly' : 'daily';
  const buildingType = info.buildingType ?? 0;
  const type = { 0: 'apartment', 1: 'villa', 2: 'house' }[buildingType] || 'apartment';
  const typeLabel = { 0: 'شقة', 1: 'فيلا', 2: 'بيت' }[buildingType] || 'عقار';
  const address = info.address || '';
  const addressParts = address.split(',').map(s => s.trim()).filter(Boolean);
  const images = info.images || [];
  const resolvedImages = images.map(img => {
    if (!img) return '';
    if (img.startsWith('http')) return img;
    return `http://45.93.137.91${img.startsWith('/') ? '' : '/'}${img}`;
  });

  return {
    id: info.id || item.propertyInformationId,
    faveId: item.id, // needed to remove from favorites
    title: `${typeLabel} في ${addressParts[0] || address}`,
    type,
    typeLabel,
    price,
    priceUnit,
    bedrooms: info.numberOfBedRooms || 0,
    bathrooms: info.numberOfBathRooms || 0,
    area: info.space || 0,
    location: {
      city: addressParts[addressParts.length - 1] || '',
      district: addressParts[0] || '',
    },
    images: resolvedImages,
    rating: item.rating || 0,
    deposit: item.deposit || 0,
  };
}

export const FavoritesProvider = ({ children }) => {
  const [favorites, setFavorites] = useState([]);
  const [isLoading, setIsLoading] = useState(false);

  const fetchFavorites = useCallback(async () => {
    if (!AuthService.isAuthenticated()) {
      setFavorites([]);
      return;
    }
    setIsLoading(true);
    try {
      const data = await getUserFavoriteProperties();
      const items = Array.isArray(data) ? data : [];
      setFavorites(items.map(mapApiFavorite));
    } catch (err) {
      console.error('[Favorites] Failed to fetch:', err);
      setFavorites([]);
    } finally {
      setIsLoading(false);
    }
  }, []);

  useEffect(() => {
    fetchFavorites();
  }, [fetchFavorites]);

  const addFavorite = async (propId) => {
    if (!AuthService.isAuthenticated()) return false;
    try {
      await addFavoriteProperty(propId);
      // Refresh to get the full object with faveId
      await fetchFavorites();
      return true;
    } catch (err) {
      console.error('[Favorites] Add failed:', err);
      return false;
    }
  };

  const removeFavorite = async (propId) => {
    if (!AuthService.isAuthenticated()) return false;
    const fav = favorites.find(f => f.id === propId);
    if (!fav) return false;
    // Optimistic update — remove immediately from UI
    const previous = [...favorites];
    setFavorites(prev => prev.filter(f => f.id !== propId));
    try {
      await removeFavoriteProperty(fav.faveId);
      return true;
    } catch (err) {
      console.error('[Favorites] Remove failed:', err);
      // Rollback on failure
      setFavorites(previous);
      return false;
    }
  };

  const isFavorite = (propId) => {
    return favorites.some(f => f.id === propId);
  };

  return (
    <FavoritesContext.Provider value={{ favorites, isLoading, addFavorite, removeFavorite, isFavorite, refreshFavorites: fetchFavorites }}>
      {children}
    </FavoritesContext.Provider>
  );
};
