"use client";

import { usePathname } from "next/navigation";
import { useTranslation } from "react-i18next";
import Link from "next/link";
import Image from "next/image";
import { NavLink, MobileNavLink } from "./components/NavLinks";
import BottomNav from './components/BottomNav';
import {
  Globe,
  LogIn,
  UserPlus,
  UserCircle,
  LogOut,
  Calendar,
  Building,
  PlusCircle,
  Heart,
  MessageCircle,
  Settings,
  Edit,
  Phone,
  Mail,
  MapPin,
  Camera,
  Bell,
  Home,
  ChevronDown,
  Menu,
  X,
  TrendingUp,
  CalendarDays,
  Clock,
  DollarSign,
  Star,
  Moon,
  Sun,
  FileText,
} from "lucide-react";
import { useState, useEffect, useRef } from "react";
import { motion, AnimatePresence } from "framer-motion";
import AuthService from "./services/AuthService";
import { UserRole, UserRoleLabels } from "./enums/UserRole";
import "./i18n/config";
import NotificationHandler from "./components/NotificationHandler";
import { useTheme } from '@/app/contexts/ThemeContext';
import { useNotifications } from '@/app/contexts/NotificationsContext';

export default function ClientLayout({ children }) {
  const { t, i18n } = useTranslation();
  const pathname = usePathname();
  const [currentLanguage, setCurrentLanguage] = useState("en");
  const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false);
  const [showUserMenu, setShowUserMenu] = useState(false);
  const [user, setUser] = useState(null);
  const [isMounted, setIsMounted] = useState(false);
  const currentYear = new Date().getFullYear();
  const menuRef = useRef(null);
  const { theme, toggleTheme } = useTheme();
  const { unreadCount } = useNotifications();

  useEffect(() => {
    setIsMounted(true);
    const savedLanguage = localStorage.getItem("language") || "ar";
    setCurrentLanguage(savedLanguage);
    i18n.changeLanguage(savedLanguage);

    if (savedLanguage === "ar") {
      document.documentElement.dir = "rtl";
      document.documentElement.lang = "ar";
    } else {
      document.documentElement.dir = "ltr";
      document.documentElement.lang = "en";
    }
  }, [i18n]);

  // Re-read user from JWT on every route change (handles post-login)
  useEffect(() => {
    const authUser = AuthService.getUser();
    if (authUser) {
      setUser({
        name: authUser.name || authUser.email,
        email: authUser.email,
        phone: authUser.phone,
        role: AuthService.isOwner() ? UserRole.OWNER : AuthService.isAgent() ? UserRole.AGENT : UserRole.CUSTOMER,
      });
    } else {
      setUser(null);
    }
  }, [pathname]);

  useEffect(() => {
    const handleClickOutside = (event) => {
      if (menuRef.current && !menuRef.current.contains(event.target)) {
        setShowUserMenu(false);
      }
    };
    document.addEventListener("mousedown", handleClickOutside);
    return () => document.removeEventListener("mousedown", handleClickOutside);
  }, []);

  const changeLanguage = (lng) => {
    i18n.changeLanguage(lng);
    setCurrentLanguage(lng);
    localStorage.setItem("language", lng);

    if (lng === "ar") {
      document.documentElement.dir = "rtl";
      document.documentElement.lang = "ar";
    } else {
      document.documentElement.dir = "ltr";
      document.documentElement.lang = "en";
    }
  };

  const toggleMobileMenu = () => {
    setIsMobileMenuOpen(!isMobileMenuOpen);
  };

  const closeMobileMenu = () => {
    setIsMobileMenuOpen(false);
  };

  const logout = () => {
    AuthService.deleteToken();
    setUser(null);
    setShowUserMenu(false);
    window.location.href = "/";
  };

  const isAuthPage = [
    "/login",
    "/blocked",
    "/register",
    "/forgot-password",
    "/auth/choose-role",
  ].includes(pathname);

  const isProfilePage = pathname === "/profile";
  const isHomePage = pathname === "/";

  const isOwner = user?.role === UserRole.OWNER;
  const isAgent = user?.role === UserRole.AGENT;
  const isOwnerOrAgent = isOwner || isAgent;
  const isCustomer = user?.role === UserRole.CUSTOMER;
  const isAuthenticated = !!user;

  const getUserInitial = () => {
    if (user?.name) {
      return user.name.charAt(0).toUpperCase();
    }
    return <UserCircle className="w-5 h-5" />;
  };

  if (!isMounted) {
    return (
      <div className="min-h-screen flex items-center justify-center">
        <div className="text-center">
          <div className="w-16 h-16 border-4 border-amber-500 border-t-transparent rounded-full animate-spin mx-auto mb-4"></div>
          <p className="text-gray-600" suppressHydrationWarning>{t("loading")}</p>
        </div>
      </div>
    );
  }

  return (
    <div className="min-h-screen flex flex-col">
      {!isAuthPage && !isAuthenticated && (
        <nav className={`fixed top-0 left-0 right-0 z-50 transition-all duration-300 ${isHomePage ? "bg-transparent backdrop-blur-sm" : "bg-white/95 backdrop-blur-sm border-b border-gray-200 shadow-sm"}`}>
          <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div
              className={`flex justify-between items-center h-20 ${currentLanguage === "ar" ? "flex-row-reverse" : ""}`}
            >
              <div className="flex items-center">
                <Link href="/" className="flex items-center space-x-3 group">
                  <div className="relative w-[150px] h-[60px]">
                    <Image
                      src="/logo.png"
                      alt={t("logoAlt")}
                      fill
                      className="object-contain"
                      priority
                    />
                  </div>
                  <span className={`text-3xl font-bold hidden md:block ${isHomePage ? "text-white" : "text-gray-800"}`}>
                    {t("brandNamePart1")}
                    <span className={isHomePage ? "text-amber-400" : "text-amber-600"}>
                      {t("brandNamePart2")}
                    </span>
                  </span>
                </Link>
              </div>

              <div className="hidden md:flex items-center space-x-4">
                <div
                  className={`flex items-center space-x-1 ${currentLanguage === "ar" ? "flex-row-reverse space-x-reverse" : ""}`}
                >
                  {/* Download App Dropdown */}
                  <div className="relative group">
                    <button className={`flex items-center gap-2 px-3 py-2 rounded-lg transition-all ${isHomePage ? "text-white/80 hover:text-amber-300 hover:bg-white/10" : "text-gray-700 hover:text-amber-600 hover:bg-amber-50"}`}>
                      <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" viewBox="0 0 16 16">
                        <path d="M11 2a3 3 0 0 0-3 3v6.5a.5.5 0 0 0 1 0V5a2 2 0 1 1 4 0v6.5a.5.5 0 0 0 1 0V5a3 3 0 0 0-3-3z"/>
                        <path d="M1.5 12.5A1.5 1.5 0 0 0 3 14h10a1.5 1.5 0 0 0 0-3H3a1.5 1.5 0 0 0-1.5 1.5z"/>
                      </svg>
                      <span className="text-sm font-semibold">{t("downloadApp")}</span>
                      <svg className="w-4 h-4 transition-transform group-hover:rotate-180" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M19 9l-7 7-7-7"/></svg>
                    </button>
                    <div className="absolute right-0 mt-2 w-64 bg-white rounded-xl shadow-xl border border-gray-200 overflow-hidden z-50 opacity-0 invisible group-hover:opacity-100 group-hover:visible transition-all duration-200 translate-y-2 group-hover:translate-y-0">
                      <div className="p-2">
                        <a href="/files/SweetHome.apk" download
                          className="flex items-center gap-3 px-4 py-3 rounded-lg hover:bg-green-50 transition-colors">
                          <div className="w-10 h-10 bg-green-100 rounded-lg flex items-center justify-center">
                            <svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" fill="#16a34a" viewBox="0 0 16 16">
                              <path d="M2.76 3.061a.5.5 0 0 1 .679.2l1.283 2.352A8.9 8.9 0 0 1 8 5a8.9 8.9 0 0 1 3.278.613l1.283-2.352a.5.5 0 1 1 .878.478l-1.252 2.295C14.475 7.266 16 9.477 16 12H0c0-2.523 1.525-4.734 3.813-5.966L2.56 3.74a.5.5 0 0 1 .2-.678ZM5 10a1 1 0 1 0 0-2 1 1 0 0 0 0 2m6 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2"/>
                            </svg>
                          </div>
                          <div>
                            <p className="font-semibold text-gray-900 text-sm">{t("android")}</p>
                            <p className="text-xs text-green-600">{t("downloadAPK")}</p>
                          </div>
                        </a>
                        <div className="flex items-center gap-3 px-4 py-3 rounded-lg opacity-50 cursor-not-allowed">
                          <div className="w-10 h-10 bg-gray-100 rounded-lg flex items-center justify-center">
                            <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="#6b7280" viewBox="0 0 16 16">
                              <path d="M11.182.008C11.148-.03 9.923.023 8.857 1.18c-1.066 1.156-.902 2.482-.878 2.516.024.034 1.52.087 2.475-1.258.955-1.345.762-2.391.728-2.43Zm3.314 11.733c-.048-.096-2.325-1.234-2.113-3.422.212-2.189 1.675-2.789 1.698-2.854.023-.065-.597-.79-1.254-1.157a3.692 3.692 0 0 0-1.563-.434c-.108-.003-.483-.095-1.254.116-.508.139-1.653.589-1.968.607-.316.018-1.256-.522-2.267-.665-.647-.125-1.333.131-1.824.328-.49.196-1.422.754-2.074 2.237-.652 1.482-.311 3.83-.067 4.56.244.729.625 1.924 1.273 2.796.576.984 1.34 1.667 1.659 1.899.319.232 1.219.385 1.843.067.502-.308 1.408-.485 1.766-.472.357.013 1.061.154 1.782.539.571.197 1.111.115 1.652-.105.541-.221 1.324-1.059 2.238-2.758.347-.79.505-1.217.473-1.282Z"/>
                            </svg>
                          </div>
                          <div>
                            <p className="font-semibold text-gray-400 text-sm">{t("ios")}</p>
                            <p className="text-xs text-gray-400">{t("comingSoon")}</p>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>

                  <NavLink light={isHomePage} href="/">{t("home")}</NavLink>
                  <NavLink light={isHomePage} href="/properties">{t("ourProperties")}</NavLink>


                  {isOwner && (
                    <>
                      <NavLink light={isHomePage} href="/owner/properties">
                        <span className="flex items-center gap-2">
                          <Building className="w-4 h-4" />
                          {t("myProperties")}
                        </span>
                      </NavLink>
                      <NavLink light={isHomePage} href="/owner/reservations">
                        <span className="flex items-center gap-2">
                          <Calendar className="w-4 h-4" />
                          {t("reservations")}
                        </span>
                      </NavLink>
                      <NavLink light={isHomePage} href="/owner/profits">
                        <span className="flex items-center gap-2">
                          <TrendingUp className="w-4 h-4" />
                          {t("profits")}
                        </span>
                      </NavLink>
                    </>
                  )}

                  {!user && !isAuthPage && (
                    <>
                      <NavLink light={isHomePage} href="/login">
                        <span className="flex items-center gap-2">
                          <LogIn className="w-4 h-4" />
                          {t("login")}
                        </span>
                      </NavLink>
                      <NavLink light={isHomePage} href="/auth/choose-role">
                        <span className="flex items-center gap-2">
                          <UserPlus className="w-4 h-4" />
                          {t("createAccount")}
                        </span>
                      </NavLink>
                    </>
                  )}
                </div>

                {/* <motion.button
                  whileHover={{ scale: 1.1, rotate: 360 }}
                  whileTap={{ scale: 0.9 }}
                  onClick={() =>
                    changeLanguage(currentLanguage === "en" ? "ar" : "en")
                  }
                  className="flex items-center justify-center w-10 h-10 bg-gray-100 hover:bg-gray-200 rounded-full transition-all duration-200 ml-4"
                >
                  <Globe className="w-5 h-5 text-gray-700" />
                </motion.button> */}

                {user && (
                  <div className="relative" ref={menuRef}>
                    <motion.button
                      onClick={() => setShowUserMenu(!showUserMenu)}
                      className="w-10 h-10 bg-gradient-to-br from-amber-500 to-amber-600 rounded-full flex items-center justify-center text-white font-bold text-lg shadow-lg hover:shadow-xl transition-all hover:scale-105 focus:outline-none"
                      whileHover={{ scale: 1.1 }}
                      whileTap={{ scale: 0.95 }}
                    >
                      {getUserInitial()}
                    </motion.button>

                    <AnimatePresence>
                      {showUserMenu && (
                        <motion.div
                          initial={{ opacity: 0, y: -10 }}
                          animate={{ opacity: 1, y: 0 }}
                          exit={{ opacity: 0, y: -10 }}
                          className="absolute left-0 mt-2 w-72 bg-white rounded-xl shadow-xl border border-gray-200 overflow-hidden z-50"
                        >
                          <div className="bg-gradient-to-r from-amber-500 to-amber-600 p-4 text-white">
                            <div className="flex items-center gap-3">
                              <div className="w-12 h-12 bg-white/20 rounded-full flex items-center justify-center text-xl font-bold">
                                {getUserInitial()}
                              </div>
                              <div>
                                <p className="font-bold">
                                  {user?.name || t("userLabel")}
                                </p>
                                <p className="text-xs text-amber-100">
                                  {user?.email || ""}
                                </p>
                                 <p className="text-xs text-amber-100 mt-1">
                                   {t(UserRoleLabels[user?.role] ? `userRole.${user?.role.toLowerCase()}` : 'visitor')}
                                 </p>
                              </div>
                            </div>
                          </div>

                          <div className="p-2">
                            <Link
                              href="/profile"
                              className="flex items-center gap-3 px-4 py-3 text-gray-700 hover:bg-amber-50 rounded-lg transition-colors"
                              onClick={() => setShowUserMenu(false)}
                            >
                              <UserCircle className="w-5 h-5 text-amber-500" />
                              <div>
                                <p className="font-medium">{t("profile")}</p>
                                <p className="text-xs text-gray-500">
                                  {t("viewAndEditInfo")}
                                </p>
                              </div>
                            </Link>

                            <Link
                              href="/favorites"
                              className="flex items-center gap-3 px-4 py-3 text-gray-700 hover:bg-amber-50 rounded-lg transition-colors"
                              onClick={() => setShowUserMenu(false)}
                            >
                              <Heart className="w-5 h-5 text-amber-500" />
                              <div>
                                <p className="font-medium">{t("favorites")}</p>
                                <p className="text-xs text-gray-500">
                                  {t("savedProperties")}
                                </p>
                              </div>
                            </Link>

                            <Link
                              href="/booked-properties"
                              className="flex items-center gap-3 px-4 py-3 text-gray-700 hover:bg-amber-50 rounded-lg transition-colors"
                              onClick={() => setShowUserMenu(false)}
                            >
                              <CalendarDays className="w-5 h-5 text-amber-500" />
                              <div>
                                <p className="font-medium">{t("myBookedProperties")}</p>
                                <p className="text-xs text-gray-500">
                                  {t("viewAndRateBooked")}
                                </p>
                              </div>
                            </Link>

                            <Link
                              href="/my-rates"
                              className="flex items-center gap-3 px-4 py-3 text-gray-700 hover:bg-amber-50 rounded-lg transition-colors"
                              onClick={() => setShowUserMenu(false)}
                            >
                              <Star className="w-5 h-5 text-amber-500" />
                              <div>
                                <p className="font-medium">{t("myRatings")}</p>
                                <p className="text-xs text-gray-500">
                                  {t("ratingsReceived")}
                                </p>
                              </div>
                            </Link>

                            <Link
                              href="/settings"
                              className="flex items-center gap-3 px-4 py-3 text-gray-700 hover:bg-amber-50 rounded-lg transition-colors"
                              onClick={() => setShowUserMenu(false)}
                            >
                              <Settings className="w-5 h-5 text-amber-500" />
                              <div>
                                <p className="font-medium">{t("settings")}</p>
                                <p className="text-xs text-gray-500">
                                  {t("accountAndSecurity")}
                                </p>
                              </div>
                            </Link>

                            {isOwner && (
                              <>
                                <div className="border-t border-gray-100 my-2"></div>

                                <Link
                                  href="/owner/properties"
                                  className="flex items-center gap-3 px-4 py-3 text-gray-700 hover:bg-amber-50 rounded-lg transition-colors"
                                  onClick={() => setShowUserMenu(false)}
                                >
                                  <Building className="w-5 h-5 text-amber-500" />
                                  <div>
                                    <p className="font-medium">{t("myProperties")}</p>
                                    <p className="text-xs text-gray-500">
                                      {t("manageYourProperties")}
                                    </p>
                                  </div>
                                </Link>

                                <Link
                                  href="/owner/properties/add"
                                  className="flex items-center gap-3 px-4 py-3 text-gray-700 hover:bg-amber-50 rounded-lg transition-colors"
                                  onClick={() => setShowUserMenu(false)}
                                >
                                  <PlusCircle className="w-5 h-5 text-amber-500" />
                                  <div>
                                    <p className="font-medium">{t("addProperty")}</p>
                                    <p className="text-xs text-gray-500">
                                      {t("addNewProperty")}
                                    </p>
                                  </div>
                                </Link>

                                <Link
                                  href="/owner/reservations"
                                  className="flex items-center gap-3 px-4 py-3 text-gray-700 hover:bg-amber-50 rounded-lg transition-colors"
                                  onClick={() => setShowUserMenu(false)}
                                >
                                  <Calendar className="w-5 h-5 text-amber-500" />
                                  <div>
                                    <p className="font-medium">{t("reservations")}</p>
                                    <p className="text-xs text-gray-500">
                                      {t("manageReservations")}
                                    </p>
                                  </div>
                                </Link>

                                <Link
                                  href="/owner/calendar"
                                  className="flex items-center gap-3 px-4 py-3 text-gray-700 hover:bg-amber-50 rounded-lg transition-colors"
                                  onClick={() => setShowUserMenu(false)}
                                >
                                  <CalendarDays className="w-5 h-5 text-amber-500" />
                                  <div>
                                    <p className="font-medium">{t("calendar")}</p>
                                    <p className="text-xs text-gray-500">
                                      {t("propertyAvailability")}
                                    </p>
                                  </div>
                                </Link>

                                <Link
                                  href="/owner/profits"
                                  className="flex items-center gap-3 px-4 py-3 text-gray-700 hover:bg-amber-50 rounded-lg transition-colors"
                                  onClick={() => setShowUserMenu(false)}
                                >
                                  <TrendingUp className="w-5 h-5 text-amber-500" />
                                  <div>
                                    <p className="font-medium">{t("profits")}</p>
                                    <p className="text-xs text-gray-500">
                                      {t("statisticsAndProfits")}
                                    </p>
                                  </div>
                                </Link>

                                <Link
                                  href="/owner/account-book"
                                  className="flex items-center gap-3 px-4 py-3 text-gray-700 hover:bg-amber-50 rounded-lg transition-colors"
                                  onClick={() => setShowUserMenu(false)}
                                >
                                  <DollarSign className="w-5 h-5 text-amber-500" />
                                  <div>
                                    <p className="font-medium">{t("accountBook")}</p>
                                    <p className="text-xs text-gray-500">
                                      {t("financialRecords")}
                                    </p>
                                  </div>
                                </Link>
                              </>
                            )}

                            {isCustomer && (
                              <>
                                <div className="border-t border-gray-100 my-2"></div>

                                <Link
                                  href="/reservations"
                                  className="flex items-center gap-3 px-4 py-3 text-gray-700 hover:bg-amber-50 rounded-lg transition-colors"
                                  onClick={() => setShowUserMenu(false)}
                                >
                                  <Calendar className="w-5 h-5 text-amber-500" />
                                  <div>
                                    <p className="font-medium">{t("myBookings")}</p>
                                    <p className="text-xs text-gray-500">
                                      {t("viewBookings")}
                                    </p>
                                  </div>
                                </Link>

                                <Link
                                  href="/booked-properties"
                                  className="flex items-center gap-3 px-4 py-3 text-gray-700 hover:bg-amber-50 rounded-lg transition-colors"
                                  onClick={() => setShowUserMenu(false)}
                                >
                                  <CalendarDays className="w-5 h-5 text-amber-500" />
                                  <div>
                                    <p className="font-medium">{t("myBookedProperties")}</p>
                                    <p className="text-xs text-gray-500">
                                      {t("viewAndRateBooked")}
                                    </p>
                                  </div>
                                </Link>

                                <Link
                                  href="/my-rates"
                                  className="flex items-center gap-3 px-4 py-3 text-gray-700 hover:bg-amber-50 rounded-lg transition-colors"
                                  onClick={() => setShowUserMenu(false)}
                                >
                                  <Star className="w-5 h-5 text-amber-500" />
                                  <div>
                                    <p className="font-medium">{t("myRatings")}</p>
                                    <p className="text-xs text-gray-500">
                                      {t("ratingsReceived")}
                                    </p>
                                  </div>
                                </Link>

                                <Link
                                  href="/reports"
                                  className="flex items-center gap-3 px-4 py-3 text-gray-700 hover:bg-amber-50 rounded-lg transition-colors"
                                  onClick={() => setShowUserMenu(false)}
                                >
                                  <FileText className="w-5 h-5 text-amber-500" />
                                  <div>
                                    <p className="font-medium">{t("reports")}</p>
                                    <p className="text-xs text-gray-500">
                                      {t("submitAndTrackReports")}
                                    </p>
                                  </div>
                                </Link>
                              </>
                            )}

                            <div className="border-t border-gray-100 my-2"></div>

                            <button
                              onClick={logout}
                              className="w-full flex items-center gap-3 px-4 py-3 text-red-600 hover:bg-red-50 rounded-lg transition-colors"
                            >
                              <LogOut className="w-5 h-5" />
                              <div>
                                <p className="font-medium">{t("logout")}</p>
                                <p className="text-xs text-red-400">
                                  {t("endSession")}
                                </p>
                              </div>
                            </button>
                          </div>
                        </motion.div>
                      )}
                    </AnimatePresence>
                  </div>
                )}
              </div>

              <div className="md:hidden flex items-center gap-3">
                <motion.button
                  whileHover={{ scale: 1.1 }}
                  whileTap={{ scale: 0.9 }}
                  onClick={toggleTheme}
                  className="flex items-center justify-center w-10 h-10 bg-gray-100 hover:bg-gray-200 rounded-full transition-colors"
                  aria-label="Toggle theme"
                >
                  {theme === 'dark' ? <Sun className="w-5 h-5 text-gray-700" /> : <Moon className="w-5 h-5 text-gray-700" />}
                </motion.button>
                <motion.button
                  whileHover={{ scale: 1.1, rotate: 360 }}
                  whileTap={{ scale: 0.9 }}
                  onClick={() =>
                    changeLanguage(currentLanguage === "en" ? "ar" : "en")
                  }
                  className="flex items-center justify-center w-10 h-10 bg-gray-100 hover:bg-gray-200 rounded-full transition-colors"
                >
                  <Globe className="w-5 h-5 text-gray-700" />
                </motion.button>

                {user && (
                  <button
                    onClick={() => setShowUserMenu(!showUserMenu)}
                    className="w-10 h-10 bg-gradient-to-br from-amber-500 to-amber-600 rounded-full flex items-center justify-center text-white font-bold shadow-lg"
                  >
                    {getUserInitial()}
                  </button>
                )}

                <button
                  type="button"
                  onClick={toggleMobileMenu}
                  className="inline-flex items-center justify-center p-2 rounded-md text-gray-700 hover:text-amber-600 hover:bg-gray-100 focus:outline-none focus:ring-2 focus:ring-inset focus:ring-amber-500"
                >
                  <span className="sr-only">{t("openMainMenu")}</span>
                  {isMobileMenuOpen ? (
                    <X className="w-6 h-6" />
                  ) : (
                    <Menu className="w-6 h-6" />
                  )}
                </button>
              </div>
            </div>
          </div>

          {isMobileMenuOpen && (
            <motion.div
              initial={{ opacity: 0, y: -10 }}
              animate={{ opacity: 1, y: 0 }}
              exit={{ opacity: 0, y: -10 }}
              className={`md:hidden border-t shadow-lg ${isHomePage ? "bg-black/60 backdrop-blur-md border-white/10" : "bg-white border-gray-200"}`}
            >
              <div className="px-2 pt-2 pb-3 space-y-1">
                <MobileNavLink light={isHomePage} href="/" onClick={closeMobileMenu}>
                  {t("home")}
                </MobileNavLink>
                <MobileNavLink light={isHomePage} href="/properties" onClick={closeMobileMenu}>
                  {t("ourProducts")}
                </MobileNavLink>

                {/* Download App - Mobile */}
                <div className={`border-t my-2 ${isHomePage ? "border-white/10" : "border-gray-200"}`}></div>
                <p className={`px-3 py-1 text-xs font-medium ${isHomePage ? "text-white/50" : "text-gray-400"}`}>{t("downloadApp")}</p>
                <a href="/files/SweetHome.apk" download onClick={closeMobileMenu}
                  className={`flex items-center gap-2 px-3 py-2 rounded-md transition-colors ${isHomePage ? "text-green-400 hover:bg-white/10" : "text-green-600 hover:bg-green-50"}`}>
                  <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" fill="currentColor" viewBox="0 0 16 16">
                    <path d="M2.76 3.061a.5.5 0 0 1 .679.2l1.283 2.352A8.9 8.9 0 0 1 8 5a8.9 8.9 0 0 1 3.278.613l1.283-2.352a.5.5 0 1 1 .878.478l-1.252 2.295C14.475 7.266 16 9.477 16 12H0c0-2.523 1.525-4.734 3.813-5.966L2.56 3.74a.5.5 0 0 1 .2-.678ZM5 10a1 1 0 1 0 0-2 1 1 0 0 0 0 2m6 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2"/>
                  </svg>
                  <span className="text-sm font-medium">{t("android")} - {t("downloadAPK")}</span>
                </a>
                <div className="flex items-center gap-2 px-3 py-2 rounded-md text-gray-400 cursor-not-allowed opacity-50">
                  <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
                    <path d="M11.182.008C11.148-.03 9.923.023 8.857 1.18c-1.066 1.156-.902 2.482-.878 2.516.024.034 1.52.087 2.475-1.258.955-1.345.762-2.391.728-2.43Zm3.314 11.733c-.048-.096-2.325-1.234-2.113-3.422.212-2.189 1.675-2.789 1.698-2.854.023-.065-.597-.79-1.254-1.157a3.692 3.692 0 0 0-1.563-.434c-.108-.003-.483-.095-1.254.116-.508.139-1.653.589-1.968.607-.316.018-1.256-.522-2.267-.665-.647-.125-1.333.131-1.824.328-.49.196-1.422.754-2.074 2.237-.652 1.482-.311 3.83-.067 4.56.244.729.625 1.924 1.273 2.796.576.984 1.34 1.667 1.659 1.899.319.232 1.219.385 1.843.067.502-.308 1.408-.485 1.766-.472.357.013 1.061.154 1.782.539.571.197 1.111.115 1.652-.105.541-.221 1.324-1.059 2.238-2.758.347-.79.505-1.217.473-1.282Z"/>
                  </svg>
                  <span className="text-sm">{t("ios")} - {t("comingSoon")}</span>
                </div>
                <div className={`border-t my-2 ${isHomePage ? "border-white/10" : "border-gray-200"}`}></div>

                {isOwner && (
                  <>
                    <MobileNavLink light={isHomePage} href="/owner/properties"
                      onClick={closeMobileMenu}
                    >
                      <span className="flex items-center gap-2">
                        <Building className="w-4 h-4" />
                        {t("myProperties")}
                      </span>
                    </MobileNavLink>
                    <MobileNavLink light={isHomePage}
                      href="/owner/reservations"
                      onClick={closeMobileMenu}
                    >
                      <span className="flex items-center gap-2">
                        <Calendar className="w-4 h-4" />
                        {t("reservations")}
                      </span>
                    </MobileNavLink>
                    <MobileNavLink light={isHomePage}
                      href="/owner/calendar"
                      onClick={closeMobileMenu}
                    >
                      <span className="flex items-center gap-2">
                        <CalendarDays className="w-4 h-4" />
                        {t("calendar")}
                      </span>
                    </MobileNavLink>
                    <MobileNavLink light={isHomePage}
                      href="/owner/profits"
                      onClick={closeMobileMenu}
                    >
                      <span className="flex items-center gap-2">
                        <TrendingUp className="w-4 h-4" />
                        {t("profits")}
                      </span>
                    </MobileNavLink>
                  </>
                )}

                {!user && (
                  <>
                    <div className={`border-t my-2 ${isHomePage ? "border-white/10" : "border-gray-200"}`}></div>
                    <MobileNavLink light={isHomePage} href="/login" onClick={closeMobileMenu}>
                      <span className="flex items-center gap-2">
                        <LogIn className="w-4 h-4" />
                        {t("login")}
                      </span>
                    </MobileNavLink>
                    <MobileNavLink light={isHomePage}
                      href="/auth/choose-role"
                      onClick={closeMobileMenu}
                    >
                      <span className="flex items-center gap-2">
                        <UserPlus className="w-4 h-4" />
                        {t("createAccount")}
                      </span>
                    </MobileNavLink>
                  </>
                )}
              </div>
            </motion.div>
          )}
        </nav>
      )}

      {isHomePage && (
        <div className="fixed inset-0 -z-10">
          <motion.div
            className="absolute inset-0 bg-cover bg-center bg-no-repeat"
            style={{ backgroundImage: 'url(/hero.jpg)' }}
            initial={{ scale: 1.1 }}
            animate={{ scale: 1 }}
            transition={{ duration: 1.5, ease: "easeOut" }}
          />
          <div className="absolute inset-0 bg-gradient-to-r from-black/70 via-black/60 to-black/50" />
        </div>
      )}

      <main
            className={`${!isAuthPage && !isProfilePage && !isAuthenticated ? "pt-20" : ""} flex-1 flex flex-col ${isHomePage ? "bg-transparent" : "bg-gray-50"} ${currentLanguage === "ar" ? "text-right" : "text-left"} ${isHomePage ? "relative" : ""} ${isAuthenticated && !isAuthPage ? "pb-20" : ""}`}
          >
            {!isAuthPage && isAuthenticated && (
              <div className={`flex items-center justify-between px-4 py-2 sticky top-0 z-20 bg-transparent backdrop-blur-3xl ${isHomePage ? "" : "border-b border-gray-200/20"}`}>
                <div className="flex items-center gap-2">
                  <span className={`text-sm font-semibold px-3 py-1 rounded-lg ${!isAuthenticated ? (isHomePage ? 'bg-white/20 text-white' : 'bg-gray-200 text-gray-600') : isOwner ? (isHomePage ? 'bg-amber-500/30 text-amber-200' : 'bg-amber-100 text-amber-700') : (isHomePage ? 'bg-blue-500/30 text-blue-200' : 'bg-blue-100 text-blue-700')}`}>
                    {!isAuthenticated ? t('visitor') : isOwner ? t('owner') : t('customer')}
                  </span>
                </div>
                <div className="flex items-center gap-3">
                  <Link href="/favorites" className={`relative group flex items-center justify-center w-9 h-9 rounded-full transition-colors hover:bg-white/10 ${pathname === '/favorites' ? 'text-amber-400' : 'nav-icon'}`}>
                    <Heart className="w-5 h-5" />
                    <div className="absolute -bottom-8 left-1/2 -translate-x-1/2 bg-gray-800 text-white text-xs rounded-lg px-2 py-1 opacity-0 group-hover:opacity-100 transition-opacity whitespace-nowrap pointer-events-none shadow-lg">
                      {t("favorites")}
                    </div>
                  </Link>
                  <Link href="/notifications" className={`relative group flex items-center justify-center w-9 h-9 rounded-full transition-colors hover:bg-white/10 ${pathname === '/notifications' ? 'text-amber-400' : 'nav-icon'}`}>
                    <Bell className="w-5 h-5" />
                    {isMounted && unreadCount > 0 && (
                      <div className="absolute -top-1 -right-1 bg-red-500 text-white text-xs font-bold rounded-full w-5 h-5 flex items-center justify-center">
                        {unreadCount > 9 ? '9+' : unreadCount}
                      </div>
                    )}
                    <div className="absolute -bottom-8 left-1/2 -translate-x-1/2 bg-gray-800 text-white text-xs rounded-lg px-2 py-1 opacity-0 group-hover:opacity-100 transition-opacity whitespace-nowrap pointer-events-none shadow-lg">
                      {t("notifications")}
                    </div>
                  </Link>
                  <Link href="/settings" className={`relative group flex items-center justify-center w-9 h-9 rounded-full transition-colors hover:bg-white/10 ${pathname === '/settings' ? 'text-amber-400' : 'nav-icon'}`}>
                    <Settings className="w-5 h-5" />
                    <div className="absolute -bottom-8 left-1/2 -translate-x-1/2 bg-gray-800 text-white text-xs rounded-lg px-2 py-1 opacity-0 group-hover:opacity-100 transition-opacity whitespace-nowrap pointer-events-none shadow-lg">
                      {t("settings")}
                    </div>
                  </Link>
                  <button onClick={toggleTheme} className="relative group flex items-center justify-center w-9 h-9 rounded-full transition-colors hover:bg-white/10 nav-icon">
                    {theme === 'dark' ? <Sun className="w-5 h-5" /> : <Moon className="w-5 h-5" />}
                    <div className="absolute -bottom-8 left-1/2 -translate-x-1/2 bg-gray-800 text-white text-xs rounded-lg px-2 py-1 opacity-0 group-hover:opacity-100 transition-opacity whitespace-nowrap pointer-events-none shadow-lg">
                      {theme === 'dark' ? t('lightMode') : t('darkMode')}
                    </div>
                  </button>
                  <button onClick={() => changeLanguage(currentLanguage === 'ar' ? 'en' : 'ar')} className="relative group flex items-center justify-center w-9 h-9 rounded-full transition-colors hover:bg-white/10 nav-icon">
                    <Globe className="w-5 h-5" />
                    <div className="absolute -bottom-8 left-1/2 -translate-x-1/2 bg-gray-800 text-white text-xs rounded-lg px-2 py-1 opacity-0 group-hover:opacity-100 transition-opacity whitespace-nowrap pointer-events-none shadow-lg">
                      {currentLanguage === 'ar' ? t('switchToEnglish') : t('switchToArabic')}
                    </div>
                  </button>
                </div>
              </div>
            )}
            <div className="flex-1">
              {children}
            </div>
            {isAuthenticated && !isAuthPage && (
              <BottomNav isOwner={isOwner} isOwnerOrAgent={isOwnerOrAgent} />
            )}
          </main>

      {pathname === "/" && (
        <footer className="bg-gray-900 text-white py-12">
          <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div
              className={`grid grid-cols-1 md:grid-cols-4 gap-8 ${currentLanguage === "ar" ? "text-right" : "text-left"}`}
            >
              <div className="space-y-4">
                <div
                  className={`flex items-center ${currentLanguage === "ar" ? "flex-row-reverse" : "space-x-3"}`}
                >
                  <div className="relative w-10 h-10">
                    <Image
                      src="/logo.png"
                      alt={t("logoAlt")}
                      fill
                      className="object-contain"
                      sizes="40px"
                    />
                  </div>
                  <span className="text-3xl font-bold">
                    {t("brandNamePart1")}
                    <span className="text-amber-400">
                      {t("brandNamePart2")}
                    </span>
                  </span>
                </div>
                <p className="text-gray-400 text-sm">
                  {t("footerDescription")}
                </p>
              </div>
              <div>
                <h3 className="text-lg font-semibold mb-4">
                  {t("quickLinks")}
                </h3>
                <ul className="space-y-2">
                  <li>
                    <Link
                      href="/"
                      className="text-gray-400 hover:text-white transition-colors block py-1"
                    >
                      {t("home")}
                    </Link>
                  </li>
                  <li>
                    <Link
                      href="/properties"
                      className="text-gray-400 hover:text-white transition-colors block py-1"
                    >
                      {t("ourProducts")}
                    </Link>
                  </li>
                </ul>
              </div>
              <div>
                <h3 className="text-lg font-semibold mb-4">{t("contactUs")}</h3>
                <ul className="space-y-3 text-gray-400">
                  <li className="flex items-center gap-2">
                    <Phone className="w-5 h-5" />
                    <span dir="ltr" className="text-right">{t("phone")}</span>
                  </li>
                  <li className="flex items-center gap-2">
                    <Mail className="w-5 h-5" />
                    <span>{t("footerEmail")}</span>
                  </li>
                </ul>
              </div>
            </div>
            <div className="mt-8 pt-8 border-t border-gray-800 text-center text-gray-400 text-sm">
              <p>
                &copy; {currentYear} {t("copyright")}. {t("allRightsReserved")}
              </p>
            </div>
          </div>
        </footer>
      )}
      <NotificationHandler />
    </div>
  );
}
