import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom';
import { Toaster } from 'react-hot-toast';
import { GuestOnly, RequireAuth, RequireSuperAdmin, RequireOwner } from './utils/guards';
import AdminLayout from './layouts/AdminLayout';
import POSLayout from './layouts/POSLayout';
import Login from './pages/Login';
import Register from './pages/Register';
import LandingPage from './pages/LandingPage';
import Dashboard from './pages/Dashboard';
import POS from './pages/POS';
import Products from './pages/Products';
import Categories from './pages/Categories';
import UsersPage from './pages/Users';
import Orders from './pages/Orders';
import KasirReport from './pages/KasirReport';
import Companies from './pages/Companies';
import Branches from './pages/Branches';
import FeeReport from './pages/FeeReport';
import Members from './pages/Members';
import Wallet from './pages/Wallet';
import Vouchers from './pages/dashboard/Vouchers';
import TopupApprovals from './pages/TopupApprovals';
import AdminTenantSettings from './pages/AdminTenantSettings';
import PrivacyPolicy from './pages/PrivacyPolicy';
import TermsOfService from './pages/TermsOfService';
import ContactSupport from './pages/ContactSupport';
import useAuthStore from './store/useAuthStore';
import { getCompanySlug } from './utils/slug';

function AuthLandingRedirect() {
  const user = useAuthStore((s) => s.user);
  if (!user) return <Navigate to="/login" replace />;
  if (user.role === 'super_admin') return <Navigate to="/admin/companies" replace />;
  const slug = getCompanySlug(user.company_name);
  if (user.role === 'owner') return <Navigate to={`/${slug}/dashboard`} replace />;
  return <Navigate to={`/${slug}/pos`} replace />;
}

export default function App() {
  return (
    <BrowserRouter>
      <Toaster
        position="top-right"
        toastOptions={{
          style: {
            background: '#FFFFFF',
            color: '#082D43',
            border: '1px solid rgba(8,45,67,0.08)',
            borderRadius: '14px',
            boxShadow: '0 8px 30px rgba(8,45,67,0.10)',
            fontSize: '13px',
            fontWeight: 500,
          },
          success: {
            iconTheme: { primary: '#0e4a6f', secondary: '#FFFFFF' },
          },
          error: {
            iconTheme: { primary: '#dc2626', secondary: '#FFFFFF' },
          },
        }}
      />

      <Routes>
        {/* Public */}
        <Route path="/" element={<LandingPage />} />
        <Route path="/login" element={<GuestOnly><Login /></GuestOnly>} />
        <Route path="/register" element={<GuestOnly><Register /></GuestOnly>} />
        <Route path="/privacy-policy" element={<PrivacyPolicy />} />
        <Route path="/terms-of-service" element={<TermsOfService />} />
        <Route path="/contact-support" element={<ContactSupport />} />

        {/* POS - accessible by all authenticated */}
        <Route element={<RequireAuth><POSLayout /></RequireAuth>}>
          <Route path="/:companySlug/pos" element={<POS />} />
        </Route>

        {/* Shared Admin - accessible by all authenticated */}
        <Route element={<RequireAuth><AdminLayout /></RequireAuth>}>
          <Route path="/:companySlug/admin/members" element={<Members />} />
          <Route path="/:companySlug/kasir/report" element={<KasirReport />} />
        </Route>

        <Route element={<RequireSuperAdmin><AdminLayout /></RequireSuperAdmin>}>
          <Route path="/admin/companies" element={<Companies />} />
          <Route path="/admin/tenant-settings" element={<AdminTenantSettings />} />
          <Route path="/admin/topups" element={<TopupApprovals />} />
          <Route path="/admin/fees" element={<FeeReport />} />
          <Route path="/orders" element={<Orders />} />
          <Route path="/reports/daily" element={<KasirReport />} />
        </Route>

        {/* Owner */}
        <Route element={<RequireOwner><AdminLayout /></RequireOwner>}>
          <Route path="/:companySlug/dashboard" element={<Dashboard />} />
          <Route path="/:companySlug/admin/branches" element={<Branches />} />
          <Route path="/:companySlug/admin/products" element={<Products />} />
          <Route path="/:companySlug/admin/categories" element={<Categories />} />
          <Route path="/:companySlug/admin/kasir" element={<UsersPage />} />
          <Route path="/:companySlug/admin/orders" element={<Orders />} />
          <Route path="/:companySlug/admin/wallet" element={<Wallet />} />
          <Route path="/:companySlug/admin/vouchers" element={<Vouchers />} />
          <Route path="/:companySlug/owner/report" element={<KasirReport />} />
        </Route>

        {/* Auth redirect fallbacks for legacy/naked paths */}
        <Route path="/dashboard" element={<RequireAuth><AuthLandingRedirect /></RequireAuth>} />
        <Route path="/pos" element={<RequireAuth><AuthLandingRedirect /></RequireAuth>} />

        {/* Default redirect */}
        <Route path="*" element={<Navigate to="/" replace />} />
      </Routes>
    </BrowserRouter>
  );
}
