/**
 * @license
 * SPDX-License-Identifier: Apache-2.0
 */

import React, { useState, useEffect } from 'react';
import {
  Network,
  Truck,
  Sliders,
  Calendar,
  FileCheck,
  Building,
  CheckCircle2,
  Clock,
  ShieldCheck,
  Send,
  HelpCircle,
  Database,
  ArrowRight,
  X,
  Search,
  Plus,
  MapPin,
  Phone,
  Activity,
  TrendingUp,
  Award
} from 'lucide-react';

import { MainSection } from './types';
import {
  useFiestaTenantLocations,
  useFiestaLocationSummary,
} from './services/fiestaQueries';
import { FIESTA_TENANT_ID, str as fstr } from './services/fiestaApi';
import Sidebar from './components/Sidebar';
import Header from './components/Header';
import DashboardView from './components/DashboardView';
import ReportsView from './components/ReportsView';
import InventoryView from './components/InventoryView';
import SettingsView from './components/SettingsView';
import StoreDetailView from './components/StoreDetailView';
import LoginView from './components/LoginView';
import UserStorePage from './components/UserStorePage';
import AwaitingApi from './components/AwaitingApi';
import type { AuthUser } from './services/auth';
import ragulStoreCover from './assets/images/store_front_view_1780299351800.png';

const AUTH_STORAGE_KEY = 'nearledaily.auth';

/** Rehydrate the signed-in user from localStorage (keeps the session across refreshes). */
function loadStoredUser(): AuthUser | null {
  try {
    const raw = localStorage.getItem(AUTH_STORAGE_KEY);
    if (!raw) return null;
    const parsed = JSON.parse(raw) as AuthUser;
    return parsed && typeof parsed === 'object' && (parsed.role === 'admin' || parsed.role === 'user')
      ? parsed
      : null;
  } catch {
    return null;
  }
}

export default function App() {
  // Auth gate — null = signed out (show login). Admin → full dashboard, user →
  // their single store console. The verified user is persisted to localStorage so
  // a refresh keeps the session (rehydrated via the lazy initializer); authRole
  // is derived from it so the two can't drift out of sync.
  const [authUser, setAuthUser] = useState<AuthUser | null>(() => loadStoredUser());
  const authRole = authUser?.role ?? null;

  // Persist (or clear) the session whenever the signed-in user changes.
  useEffect(() => {
    try {
      if (authUser) localStorage.setItem(AUTH_STORAGE_KEY, JSON.stringify(authUser));
      else localStorage.removeItem(AUTH_STORAGE_KEY);
    } catch {
      /* storage unavailable (private mode / quota) — session just won't persist */
    }
  }, [authUser]);

  const handleLogin = (user: AuthUser) => setAuthUser(user);

  const currentUser = {
    name: authUser?.name || 'Account',
    role: authRole === 'admin' ? 'Operations Admin' : 'Team Member',
    email: authUser?.email || '—',
  };

  // Navigation indicators states
  const [currentSection, setCurrentSection] = useState<MainSection>('dashboard');
  const [selectedStore, setSelectedStore] = useState<{ locationid?: number; name: string; zone: string; deliveries: number; sales: string; orders?: number; staff: string; color: string; status: string } | null>(null);
  
  const handleSetSection = (sec: MainSection) => {
    setCurrentSection(sec);
    setSelectedStore(null);
  };

  const [isCoimbatoreView, setIsCoimbatoreView] = useState(false);
  const [searchQuery, setSearchQuery] = useState('');
  const [sidebarOpen, setSidebarOpen] = useState(true);

  // Scope every Fiesta query to the signed-in merchant. The login record carries
  // the user's tenantid; fall back to the shared constant only when it's absent
  // (e.g. a legacy session before tenantid was captured) so the page still loads.
  const tenantId = authUser?.tenantid || FIESTA_TENANT_ID;

  // ── Live data for the secondary sections (Fiesta) ─────────────────────────
  // Stores ← tenant locations + per-location order summary (seeded into local
  // state so the "Add Store" handler keeps working). Users management now lives
  // under Settings → Users & Access (see UsersPanel).
  const locationsQ = useFiestaTenantLocations(tenantId);
  const locSummaryQ = useFiestaLocationSummary(tenantId);

  const STORE_COVERS = [
    'https://images.unsplash.com/photo-1542838132-92c53300491e?auto=format&fit=crop&w=600&q=80',
    'https://images.unsplash.com/photo-1578916171728-46686eac8d58?auto=format&fit=crop&w=600&q=80',
    'https://images.unsplash.com/photo-1604719312566-8912e9227c6a?auto=format&fit=crop&w=600&q=80',
    'https://images.unsplash.com/photo-1534723452862-4c874018d66d?auto=format&fit=crop&w=600&q=80',
    'https://images.unsplash.com/photo-1582408929130-98a2c2640b8a?auto=format&fit=crop&w=600&q=80',
    'https://images.unsplash.com/photo-1516594798947-e65505dbb29d?auto=format&fit=crop&w=600&q=80',
    'https://images.unsplash.com/photo-1601599561263-60a4e4e083cd?auto=format&fit=crop&w=600&q=80',
    'https://images.unsplash.com/photo-1441986300917-64674bd600d8?auto=format&fit=crop&w=600&q=80',
    'https://images.unsplash.com/photo-1528698827591-e19ccd7bc23d?auto=format&fit=crop&w=600&q=80',
    'https://images.unsplash.com/photo-1536697246787-1f7ae568d89a?auto=format&fit=crop&w=600&q=80',
    'https://images.unsplash.com/photo-1506617498306-bd97b3663b65?auto=format&fit=crop&w=600&q=80',
    'https://images.unsplash.com/photo-1579621970563-ebec7560ff3e?auto=format&fit=crop&w=600&q=80',
  ];

  const getStoreCover = (name: string) => {
    if (name.toLowerCase().includes('ragul')) return ragulStoreCover;
    let hash = 0;
    for (let j = 0; j < name.length; j++) {
      hash = name.charCodeAt(j) + ((hash << 5) - hash);
    }
    const idx = Math.abs(hash) % STORE_COVERS.length;
    return STORE_COVERS[idx];
  };

  // Dynamic Secondary Modules list states (seeded from live data once it loads).
  const [storesList, setStoresList] = useState<Array<{ locationid?: number; name: string; zone: string; deliveries: number; sales: string; orders: number; staff: string; color: string; status: string }>>([]);

  const [storesSearch, setStoresSearch] = useState('');
  const [storesFilter, setStoresFilter] = useState<'ALL' | 'ACTIVE' | 'CRITICAL'>('ALL');

  const filteredStoresList = storesList.filter((st) => {
    const q = storesSearch.trim().toLowerCase();
    // Match across every field shown on the card — name, zone, manager/contact,
    // and the outlet id — coercing each to a string so a missing/numeric value
    // never throws and silently breaks the whole filter.
    const haystack = [st.name, st.zone, st.staff, st.locationid]
      .map((v) => String(v ?? '').toLowerCase())
      .join(' ');
    const matchesSearch = !q || haystack.includes(q);

    if (storesFilter === 'ACTIVE') {
      return matchesSearch && st.status.toLowerCase() === 'active';
    }
    if (storesFilter === 'CRITICAL') {
      return (
        matchesSearch &&
        (st.status.toLowerCase() !== 'active' || st.deliveries > 40)
      );
    }
    return matchesSearch;
  });

  const activeCount = storesList.filter((st) => st.status.toLowerCase() === 'active').length;
  const totalCount = storesList.length;
  const totalDeliveries = storesList.reduce((acc, st) => acc + st.deliveries, 0);

  useEffect(() => {
    const locations = locationsQ.data ?? [];
    const summaries = locSummaryQ.data ?? [];
    if (locations.length) {
      setStoresList(
        locations.map((loc) => {
          const sum = summaries.find((s) => s.locationid === Number(loc.locationid));
          return {
            locationid: Number(loc.locationid),
            name: fstr(loc.locationname) || `Location ${fstr(loc.locationid)}`,
            zone: [fstr(loc.suburb), fstr(loc.city)].filter(Boolean).join(', ') || 'Coimbatore',
            deliveries: sum?.delivered ?? 0,
            sales: `${(sum?.total ?? 0).toLocaleString('en-IN')} orders`,
            orders: Math.max(sum?.delivered ?? 0, sum?.total ?? 0),
            staff: fstr(loc.contactno) || fstr(loc.email) || '—',
            color: fstr(loc.status).toLowerCase() === 'active' ? 'emerald' : 'amber',
            status: fstr(loc.status) || 'Active',
          };
        }),
      );
    }
  }, [locationsQ.data, locSummaryQ.data]);

  // Secondary sub-sections modals triggers
  const [showAddStoreModal, setShowAddStoreModal] = useState(false);

  // New forms states
  const [newStore, setNewStore] = useState({ name: '', zone: '', lead: '', sales: '₹1,50,000' });

  // Form submission handles for secondary sections
  const handleCreateStore = (e: React.FormEvent) => {
    e.preventDefault();
    if (!newStore.name || !newStore.zone || !newStore.lead) {
      alert('Kindly fill store metadata completely.');
      return;
    }
    setStoresList([...storesList, {
      locationid: 10000 + Math.floor(Math.random() * 9000),
      name: newStore.name,
      zone: newStore.zone,
      deliveries: 0,
      sales: '0 orders',
      orders: 0,
      staff: newStore.lead,
      color: 'emerald',
      status: 'Active'
    }]);
    setShowAddStoreModal(false);
    setNewStore({ name: '', zone: '', lead: '', sales: '₹1,50,000' });
    alert(`Node outlet "${newStore.name}" commissioned to live operations feed successfully.`);
  };

  // Calendar Event Modal state
  const [showCalendarModal, setShowCalendarModal] = useState(false);

  // Callback action triggers
  const handleHelp = () => {
    alert('nearledaily User Manual & Documentation Center linked successfully. Contact Coimbatore regional IT hub desk for urgent escalations.');
  };

  const handleLogout = () => setAuthUser(null);

  // Define secondary sections (Stores, Logistics, Staffing, Settings) within main body
  const renderSecondarySection = () => {
    switch (currentSection) {
      case 'stores': {
        // A single-store merchant has no branches, so skip the one-card registry
        // and open that store directly. Add a branch (2+ stores) and the grid
        // returns automatically. Clicking a card in multi-store mode also opens
        // the console, with a Back-to-registry button.
        const isSoleStore = !selectedStore && storesList.length === 1;
        const activeStore = selectedStore ?? (isSoleStore ? storesList[0] : null);

        if (activeStore) {
          return (
            <div className="space-y-md animate-in fade-in duration-300">
              {isSoleStore && (
                <div className="flex flex-col md:flex-row md:items-center justify-between gap-4 pb-4 border-b border-zinc-205">
                  <div>
                    <h1 className="font-sans font-bold text-2xl tracking-tight text-[#0f172a]">
                      Store Console
                    </h1>
                    <p className="text-zinc-500 font-sans text-xs mt-1">
                      This merchant operates a single store. Add a branch to manage multiple outlets.
                    </p>
                  </div>
                  <button
                    onClick={() => setShowAddStoreModal(true)}
                    className="bg-[#581c87] text-white px-5 py-2.5 rounded-lg text-xs font-bold uppercase tracking-wider flex items-center justify-center gap-2 cursor-pointer hover:bg-purple-800 transition shadow-sm self-start md:self-auto"
                  >
                    <Plus className="w-4 h-4" />
                    Add Retail Outlet Node
                  </button>
                </div>
              )}
              <StoreDetailView
                store={activeStore}
                onBack={selectedStore ? () => setSelectedStore(null) : undefined}
                tenantId={tenantId}
              />
            </div>
          );
        }

        return (
          <div className="space-y-lg animate-in fade-in duration-300">
            {/* Simple and elegant premium header */}
            <div className="flex flex-col md:flex-row md:items-center justify-between gap-4 pb-6 border-b border-zinc-205 mb-6">
              <div>
                <h1 className="font-sans font-bold text-2xl tracking-tight text-[#0f172a]">
                  Stores Registry
                </h1>
                <p className="text-zinc-500 font-sans text-xs mt-1">
                  Local nodes registry, active manager assignments, and live dispatch and grocery delivery fulfillment statistics.
                </p>
              </div>

              <button
                onClick={() => setShowAddStoreModal(true)}
                className="bg-[#581c87] text-white px-5 py-2.5 rounded-lg text-xs font-bold uppercase tracking-wider flex items-center justify-center gap-2 cursor-pointer hover:bg-purple-800 transition shadow-sm"
              >
                <Plus className="w-4 h-4" />
                Add Retail Outlet Node
              </button>
            </div>

            {/* Filter control bar */}
            <div className="flex flex-col md:flex-row items-center justify-between gap-4 bg-white p-4 rounded-xl border border-[#e2e8f0] shadow-sm mb-6">
              {/* Search input with search icon */}
              <div className="relative w-full md:max-w-md">
                <span className="absolute inset-y-0 left-0 pl-3.5 flex items-center pointer-events-none text-zinc-400">
                  <Search className="w-4 h-4" />
                </span>
                <input
                  type="text"
                  placeholder="Search stores by name, zone, or manager..."
                  value={storesSearch}
                  onChange={(e) => setStoresSearch(e.target.value)}
                  className="w-full pl-10 pr-4 py-2 bg-zinc-50 border border-zinc-200 rounded-lg text-xs font-medium text-zinc-800 placeholder-zinc-400 focus:outline-none focus:ring-2 focus:ring-[#581c87]/20 focus:border-[#581c87] transition-all"
                />
                {storesSearch && (
                  <button
                    onClick={() => setStoresSearch('')}
                    className="absolute inset-y-0 right-0 pr-3 flex items-center text-zinc-400 hover:text-zinc-600"
                  >
                    <X className="w-3.5 h-3.5" />
                  </button>
                )}
              </div>

              {/* Filter tabs */}
              <div className="flex items-center bg-zinc-100/80 p-1 rounded-lg border border-zinc-200/50 w-full md:w-auto">
                <button
                  onClick={() => setStoresFilter('ALL')}
                  className={`flex-1 md:flex-none px-4 py-1.5 rounded-md text-xs font-bold transition-all ${
                    storesFilter === 'ALL'
                      ? 'bg-white text-[#581c87] shadow-sm'
                      : 'text-zinc-500 hover:text-zinc-800 hover:bg-white/40'
                  }`}
                >
                  All Nodes ({storesList.length})
                </button>
                <button
                  onClick={() => setStoresFilter('ACTIVE')}
                  className={`flex-1 md:flex-none px-4 py-1.5 rounded-md text-xs font-bold transition-all ${
                    storesFilter === 'ACTIVE'
                      ? 'bg-white text-emerald-600 shadow-sm'
                      : 'text-zinc-500 hover:text-emerald-600 hover:bg-white/40'
                  }`}
                >
                  Active ({storesList.filter(s => s.status.toLowerCase() === 'active').length})
                </button>
                <button
                  onClick={() => setStoresFilter('CRITICAL')}
                  className={`flex-1 md:flex-none px-4 py-1.5 rounded-md text-xs font-bold transition-all ${
                    storesFilter === 'CRITICAL'
                      ? 'bg-white text-rose-600 shadow-sm'
                      : 'text-zinc-500 hover:text-rose-650 hover:bg-white/40'
                  }`}
                >
                  Critical Alerts ({storesList.filter(s => s.status.toLowerCase() !== 'active' || s.deliveries > 40 || s.deliveries === 0).length})
                </button>
              </div>
            </div>

            {/* Empty States */}
            {filteredStoresList.length === 0 && (
              <div className="text-center py-12 text-zinc-400 text-xs border border-dashed border-[#e2e8f0] rounded-xl bg-white p-8">
                {locationsQ.isLoading ? (
                  <div className="flex flex-col items-center justify-center gap-2">
                    <div className="w-6 h-6 border-2 border-[#581c87] border-t-transparent rounded-full animate-spin" />
                    <span>Loading live store locations…</span>
                  </div>
                ) : (
                  <span>No store locations found matching your filter criteria.</span>
                )}
              </div>
            )}

            {/* Immersive Background Blur Blobs */}
            <div className="relative">
              <div className="absolute top-10 left-10 w-72 h-72 bg-purple-400/10 rounded-full blur-[100px] pointer-events-none -z-10 animate-pulse" />
              <div className="absolute bottom-10 right-10 w-80 h-80 bg-indigo-400/10 rounded-full blur-[100px] pointer-events-none -z-10 animate-pulse" style={{ animationDuration: '6s' }} />

              {/* Store Cards Grid */}
              <div className="grid grid-cols-1 md:grid-cols-3 gap-gutter relative z-10">
                {filteredStoresList.map((st, i) => {
                  const totalOrders = st.orders ?? st.deliveries ?? 0;
                  const fulfillmentRate = totalOrders > 0 ? Math.min(100, Math.round((st.deliveries / totalOrders) * 100)) : 100;
                  
                  return (
                    <div
                      key={st.locationid || i}
                      onClick={() => setSelectedStore(st)}
                      className={`group relative overflow-hidden bg-white/70 backdrop-blur-md border border-zinc-200/80 rounded-2xl shadow-sm hover:shadow-[0_20px_40px_rgba(88,28,135,0.12)] transition-all duration-500 cursor-pointer flex flex-col ${
                        st.status.toLowerCase() === 'active'
                          ? st.deliveries > 40
                            ? 'hover:border-rose-300'
                            : 'hover:border-emerald-300'
                          : 'hover:border-amber-300'
                      }`}
                    >
                      {/* Card Cover Image with Zoom effect */}
                      <div className="relative h-32 w-full overflow-hidden flex-shrink-0">
                        <img
                          src={getStoreCover(st.name)}
                          alt={st.name}
                          className="w-full h-full object-cover group-hover:scale-108 transition-transform duration-700 ease-out"
                        />
                        <div className="absolute inset-0 bg-gradient-to-t from-slate-900/90 via-slate-900/20 to-transparent" />
                        
                        {/* Status Badge */}
                        <div className="absolute top-3 right-3">
                          <span className={`px-2.5 py-0.5 rounded-full text-[9px] font-bold uppercase tracking-wider flex items-center gap-1 border backdrop-blur-md ${
                            st.status.toLowerCase() === 'active'
                              ? st.deliveries > 40
                                ? 'text-rose-200 bg-rose-950/60 border-rose-500/30'
                                : 'text-emerald-200 bg-emerald-950/60 border-emerald-500/30'
                              : 'text-amber-200 bg-amber-950/60 border-amber-500/30'
                          }`}>
                            <span className="w-1.5 h-1.5 rounded-full bg-current animate-pulse" />
                            {st.status.toLowerCase() === 'active' && st.deliveries > 40 ? 'High Load' : st.status}
                          </span>
                        </div>

                        {/* Zone & Title */}
                        <div className="absolute bottom-3 left-3 right-3 text-white">
                          <p className="text-[9px] text-purple-200 font-bold uppercase tracking-widest leading-none mb-1">{st.zone}</p>
                          <h3 className="font-sans font-bold text-sm leading-tight text-white group-hover:text-purple-200 transition-colors">{st.name}</h3>
                        </div>
                      </div>

                      {/* Card Content Area */}
                      <div className="p-5 flex-1 flex flex-col justify-between">
                        {/* Metrics Row & Progress Circle */}
                        <div className="flex items-center justify-between gap-4">
                          <div className="grid grid-cols-2 gap-4 flex-1">
                            <div>
                              <span className="text-[9px] text-zinc-400 uppercase tracking-widest font-bold block">Deliveries</span>
                              <p className="font-extrabold text-base text-slate-900 mt-0.5 font-mono">{st.deliveries.toLocaleString()}</p>
                              <span className="text-[8px] text-emerald-600 font-semibold block mt-0.5">Dispatched Today</span>
                            </div>
                            <div>
                              <span className="text-[9px] text-zinc-400 uppercase tracking-widest font-bold block">Total Orders</span>
                              <p className="font-extrabold text-base text-[#581c87] mt-0.5 font-mono">{totalOrders.toLocaleString()}</p>
                              <span className="text-[8px] text-purple-600 font-semibold block mt-0.5">Incoming Volume</span>
                            </div>
                          </div>

                          {/* Circular Progress Ring */}
                          <div className="relative flex items-center justify-center flex-shrink-0" title={`Fulfillment Rate: ${fulfillmentRate}%`}>
                            <svg className="w-12 h-12 transform -rotate-90">
                              <circle
                                cx="24"
                                cy="24"
                                r="18"
                                className="stroke-zinc-100"
                                strokeWidth="3.5"
                                fill="transparent"
                              />
                              <circle
                                cx="24"
                                cy="24"
                                r="18"
                                className={`transition-all duration-500 ${
                                  st.status.toLowerCase() === 'active'
                                    ? st.deliveries > 40
                                      ? 'stroke-rose-500'
                                      : 'stroke-emerald-500'
                                    : 'stroke-amber-500'
                                }`}
                                strokeWidth="3.5"
                                fill="transparent"
                                strokeDasharray="113"
                                strokeDashoffset={113 - (113 * fulfillmentRate) / 100}
                                strokeLinecap="round"
                              />
                            </svg>
                            <span className="absolute text-[9px] font-extrabold text-zinc-700 font-mono">
                              {fulfillmentRate}%
                            </span>
                          </div>
                        </div>

                        {/* Live Sparkline Trend Histogram */}
                        <div className="mt-4 pt-3.5 border-t border-zinc-100">
                          <div className="flex justify-between items-center text-[8px] text-zinc-400 font-bold uppercase tracking-widest mb-1.5">
                            <span>Speed Index (Live Feed)</span>
                            <span className="text-[#581c87] flex items-center gap-0.5 text-[8px] font-bold">
                              <Activity className="w-2.5 h-2.5 animate-pulse" /> Live
                            </span>
                          </div>
                          <div className="h-6 flex items-end gap-1">
                            {[30, 48, 25, 62, 54, 75, 42, 80, Math.min(95, Math.max(15, st.deliveries * 1.8))].map((val, idx) => (
                              <div key={idx} className="flex-1 bg-zinc-100 rounded-t-sm h-full relative overflow-hidden group/bar">
                                <div
                                  style={{ height: `${val}%` }}
                                  className={`absolute bottom-0 left-0 right-0 rounded-t-sm transition-all duration-500 ${
                                    st.status.toLowerCase() === 'active'
                                      ? st.deliveries > 40
                                        ? 'bg-rose-500/80 group-hover/bar:bg-rose-500'
                                        : 'bg-[#581c87]/70 group-hover/bar:bg-[#581c87]'
                                      : 'bg-amber-500/70 group-hover/bar:bg-amber-500'
                                  }`}
                                />
                              </div>
                            ))}
                          </div>
                        </div>

                        {/* Lead Manager Profile block */}
                        <div className="flex justify-between items-center text-xs text-zinc-650 bg-zinc-50/80 rounded-xl p-2.5 border border-zinc-100/80 mt-4">
                          <div className="flex items-center gap-2">
                            <div className="w-7 h-7 rounded-full bg-gradient-to-tr from-purple-600 to-indigo-600 text-white flex items-center justify-center font-bold text-[10px] border border-white shadow-sm flex-shrink-0">
                              {st.staff.slice(0, 2).toUpperCase()}
                            </div>
                            <div className="min-w-0">
                              <span className="text-[8px] text-zinc-400 block font-semibold leading-none mb-0.5">Node Lead</span>
                              <span className="font-bold text-[#0f172a] text-xs truncate max-w-[100px] block leading-none">{st.staff}</span>
                            </div>
                          </div>
                          
                          <button 
                            onClick={(e) => {
                              e.stopPropagation();
                              alert(`Routing communications channel directly to manager ${st.staff}...`);
                            }}
                            className="p-1.5 rounded-lg bg-white border border-zinc-200 hover:border-[#581c87] hover:text-[#581c87] text-zinc-500 transition-colors shadow-sm"
                            title="Communicate with Node Lead"
                          >
                            <Phone className="w-3.5 h-3.5" />
                          </button>
                        </div>

                        {/* Card footer - enter console */}
                        <div className="flex items-center justify-between text-[10px] font-bold text-[#581c87] mt-4 pt-3 border-t border-zinc-100/80">
                          <span className="uppercase tracking-wider">Enter Terminal Console</span>
                          <ArrowRight className="w-3.5 h-3.5 transform group-hover:translate-x-1.5 transition-transform duration-350 text-[#581c87]" />
                        </div>
                      </div>
                    </div>
                  );
                })}
              </div>
            </div>
          </div>
        );
      }

      case 'settings':
        return <SettingsView tenantId={tenantId} user={authUser ?? undefined} />;

      default:
        return null;
    }
  };

  // ── Auth gate ──────────────────────────────────────────────────────────────
  // Signed out → login screen. User role → their single allocated store console
  // (scoped by applocationid). Only the admin role reaches the full operations
  // dashboard with the all-stores registry below.
  if (authRole === null) {
    return <LoginView onLogin={handleLogin} />;
  }
  if (authRole === 'user' && authUser) {
    return <UserStorePage onLogout={handleLogout} user={authUser} />;
  }

  return (
    <div className="min-h-screen bg-[#f8fafc] text-[#0f172a] font-sans antialiased">
      {/* Navbar segment */}
      <Header
        currentSection={currentSection}
        setCurrentSection={handleSetSection}
        isCoimbatoreView={isCoimbatoreView}
        onToggleSidebar={() => setSidebarOpen((prev) => !prev)}
        isSidebarOpen={sidebarOpen}
        onHelpClick={handleHelp}
        onLogoutClick={handleLogout}
        profile={currentUser}
      />

      {/* Main Container workspace layout splits */}
      <div className="flex pt-20">

        {/* Interactive Left Rail */}
        <Sidebar
          currentSection={currentSection}
          setCurrentSection={handleSetSection}
          isCoimbatoreView={isCoimbatoreView}
          setIsCoimbatoreView={setIsCoimbatoreView}
          isOpen={sidebarOpen}
          isAdmin={authRole === 'admin'}
        />

        {/* Main core pages payload area */}
        <main className={`flex-1 min-w-0 min-h-[calc(100vh-80px)] transition-all duration-300 ${sidebarOpen ? 'md:pl-64' : 'md:pl-20'}`}>
          <div className="w-full p-container-margin md:p-xl space-y-lg transition-all duration-300">
            {/* Nav content routing */}
            {currentSection === 'dashboard' && (
              <DashboardView searchQuery={searchQuery} isCoimbatoreView={isCoimbatoreView} tenantId={tenantId} />
            )}

            {currentSection === 'inventory' && (
              <InventoryView
                searchQuery={searchQuery}
                isCoimbatoreView={isCoimbatoreView}
                tenantId={tenantId}
              />
            )}

            {currentSection === 'reports' && (
              <ReportsView
                searchQuery={searchQuery}
                isCoimbatoreView={isCoimbatoreView}
                setIsCoimbatoreView={setIsCoimbatoreView}
                tenantId={tenantId}
              />
            )}


            {/* Handle alternative sections: Stores, Settings */}
            {['stores', 'settings'].includes(currentSection) &&
              renderSecondarySection()
            }
          </div>
        </main>
      </div>

      {/* CALENDAR SCHEDULER DIALOG MODAL */}
      {showCalendarModal && (
        <div 
          className="fixed inset-0 bg-[#0f172a]/40 backdrop-blur-sm z-[200] flex items-center justify-center p-md"
          onClick={(e) => { if (e.target === e.currentTarget) setShowCalendarModal(false); }}
        >
          <div className="bg-white border border-[#e2e8f0] rounded-xl w-full max-w-[24rem] max-h-[90vh] flex flex-col shadow-2xl overflow-hidden animate-in zoom-in-95 duration-200 text-xs font-sans cursor-default">
            <div className="p-md border-b border-[#e2e8f0] bg-[#f8fafc] flex justify-between items-center shrink-0">
              <h4 className="font-bold text-[#0f172a] flex items-center gap-xs">
                <Calendar size={15} className="text-[#581c87]" />
                Scheduled Reports Calendar
              </h4>
              <button 
                onClick={() => setShowCalendarModal(false)}
                className="p-1 hover:bg-zinc-200 rounded-full text-zinc-400 cursor-pointer transition-colors"
              >
                <X size={16} />
              </button>
            </div>

            <div className="p-md space-y-md overflow-y-auto flex-1">
              <p className="text-zinc-500 leading-relaxed font-semibold">
                Automated compliance summaries will appear here once scheduled-report exports are available.
              </p>

              <AwaitingApi label="Scheduled reports" api="[R13]" />
            </div>

            <div className="p-sm bg-[#f8fafc] border-t border-[#e2e8f0] flex justify-end shrink-0">
              <button
                onClick={() => setShowCalendarModal(false)}
                className="px-4 py-2 bg-[#0f172a] text-white rounded-lg font-semibold hover:bg-zinc-800 cursor-pointer shadow-sm transition-colors"
              >
                Close Calendar
              </button>
            </div>
          </div>
        </div>
      )}

      {/* CREATE NEW STORE MODAL */}
      {showAddStoreModal && (
        <div 
          className="fixed inset-0 bg-[#0f172a]/40 backdrop-blur-sm z-[200] flex items-center justify-center p-md"
          onClick={(e) => { if (e.target === e.currentTarget) setShowAddStoreModal(false); }}
        >
          <div className="bg-white border border-[#e2e8f0] rounded-xl w-full max-w-[24rem] max-h-[90vh] flex flex-col shadow-2xl overflow-hidden animate-in zoom-in-95 duration-200 text-xs font-sans cursor-default">
            <div className="p-md border-b border-[#e2e8f0] bg-[#f8fafc] flex justify-between items-center shrink-0">
              <h4 className="font-bold text-[#0f172a] flex items-center gap-xs">
                <Building size={15} className="text-[#581c87]" />
                Commission New Regional Store Node
              </h4>
              <button 
                onClick={() => setShowAddStoreModal(false)}
                className="p-1 hover:bg-zinc-200 rounded-full text-zinc-400 cursor-pointer transition-colors"
              >
                <X size={16} />
              </button>
            </div>

            <form onSubmit={handleCreateStore} className="flex-1 flex flex-col min-h-0 overflow-hidden">
              <div className="p-md space-y-md overflow-y-auto flex-1">
                <div className="space-y-sm">
                  <div className="space-y-1">
                    <label className="font-bold text-zinc-500 uppercase tracking-widest text-[9px]">STORE OUTLET NAME (*)</label>
                    <input 
                      type="text" 
                      placeholder="e.g. RS Puram Super Hub" 
                      value={newStore.name}
                      onChange={(e) => setNewStore({ ...newStore, name: e.target.value })}
                      className="w-full border border-[#e2e8f0] rounded-lg p-sm bg-[#f8fafc] focus:bg-white outline-none focus:ring-1 focus:ring-[#581c87]"
                      required
                    />
                  </div>

                  <div className="space-y-1">
                    <label className="font-bold text-zinc-500 uppercase tracking-widest text-[9px]">LOCAL ZONE AREA (*)</label>
                    <input 
                      type="text" 
                      placeholder="e.g. Coimbatore North" 
                      value={newStore.zone}
                      onChange={(e) => setNewStore({ ...newStore, zone: e.target.value })}
                      className="w-full border border-[#e2e8f0] rounded-lg p-sm bg-[#f8fafc] focus:bg-white outline-none focus:ring-1 focus:ring-[#581c87]"
                      required
                    />
                  </div>

                  <div className="space-y-1">
                    <label className="font-bold text-zinc-500 uppercase tracking-widest text-[9px]">OUTLET TEAM MANAGER (*)</label>
                    <input 
                      type="text" 
                      placeholder="e.g. Sridhar Sundaram" 
                      value={newStore.lead}
                      onChange={(e) => setNewStore({ ...newStore, lead: e.target.value })}
                      className="w-full border border-[#e2e8f0] rounded-lg p-sm bg-[#f8fafc] focus:bg-white outline-none focus:ring-1 focus:ring-[#581c87]"
                      required
                    />
                  </div>

                  <div className="space-y-1">
                    <label className="font-bold text-zinc-500 uppercase tracking-widest text-[9px]">ESTIMATED INITIAL REVENUE</label>
                    <input 
                      type="text" 
                      placeholder="₹1,50,000" 
                      value={newStore.sales}
                      onChange={(e) => setNewStore({ ...newStore, sales: e.target.value })}
                      className="w-full border border-[#e2e8f0] rounded-lg p-sm bg-[#f8fafc] focus:bg-white outline-none focus:ring-1 focus:ring-[#581c87]"
                    />
                  </div>
                </div>
              </div>

              <div className="p-md border-t border-[#f1f5f9] flex justify-end gap-sm bg-[#f8fafc] shrink-0">
                <button
                  type="button"
                  onClick={() => setShowAddStoreModal(false)}
                  className="px-4 py-2 border border-[#e2e8f0] rounded-lg font-semibold text-zinc-500 hover:bg-zinc-50 cursor-pointer"
                >
                  Cancel
                </button>
                <button
                  type="submit"
                  className="px-4 py-2 bg-[#581c87] text-white rounded-lg font-bold hover:bg-purple-800 cursor-pointer shadow-sm"
                >
                  Create Outlet Node
                </button>
              </div>
            </form>
          </div>
        </div>
      )}

    </div>
  );
}
