import {
  createRouter,
  createRootRoute,
  createRoute,
  Outlet,
  notFound,
} from '@tanstack/react-router';
import { Typography } from 'antd';
import { AppShell } from '../components/layout/AppShell';
import { RepPainel } from '../cockpits/rep/RepPainel';
import { ClientsPage } from '../cockpits/rep/ClientsPage';
import { ClientDetailPage } from '../cockpits/rep/ClientDetailPage';
import { OrdersPage } from '../cockpits/rep/OrdersPage';
import { OrderDetailPage } from '../cockpits/rep/OrderDetailPage';
import { OrderPrintPage } from '../cockpits/rep/OrderPrintPage';
import { NewClientPage } from '../cockpits/rep/NewClientPage';
import { NewOrderPage } from '../cockpits/rep/NewOrderPage';
import { CatalogPage } from '../cockpits/rep/CatalogPage';
import { ApprovalQueuePage } from '../cockpits/supervisor/ApprovalQueuePage';
import { SupervisorPainel } from '../cockpits/supervisor/SupervisorPainel';
import { authStore } from './auth-store';

function getRoleFromToken(): string {
  const token = authStore.get();
  if (!token) return 'rep';
  try {
    const payload = JSON.parse(atob(token.split('.')[1] ?? ''));
    return (payload.role as string) ?? 'rep';
  } catch {
    return 'rep';
  }
}

function HomeRoute() {
  const role = getRoleFromToken();
  return role === 'supervisor' || role === 'manager' ? <SupervisorPainel /> : <RepPainel />;
}

function NotFoundPage() {
  return (
    <div style={{ padding: 48, textAlign: 'center' }}>
      <Typography.Title level={3} type="secondary">
        Página não encontrada
      </Typography.Title>
    </div>
  );
}

// Suprime aviso de notFound não utilizado — usado via throw notFound() em loaders futuros.
void notFound;

const rootRoute = createRootRoute({
  component: () => (
    <AppShell>
      <Outlet />
    </AppShell>
  ),
  notFoundComponent: NotFoundPage,
});

const indexRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/',
  component: HomeRoute,
});

const rafaelRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/rep',
  component: RepPainel,
});

const clientesRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/clientes',
  component: ClientsPage,
});

const novoClienteRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/clientes/novo',
  component: NewClientPage,
});

const clienteDetailRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/clientes/$id',
  component: ClientDetailPage,
});

const pedidosRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/pedidos',
  component: OrdersPage,
});

const novoOrderRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/pedidos/novo',
  component: NewOrderPage,
});

const pedidoDetailRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/pedidos/$id',
  component: OrderDetailPage,
});

const pedidoPrintRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/pedidos/$id/imprimir',
  component: OrderPrintPage,
});

const catalogoRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/catalogo',
  component: CatalogPage,
});

const aprovacoes = createRoute({
  getParentRoute: () => rootRoute,
  path: '/aprovacoes',
  component: ApprovalQueuePage,
});

const routeTree = rootRoute.addChildren([
  indexRoute,
  rafaelRoute,
  clientesRoute,
  novoClienteRoute,
  clienteDetailRoute,
  pedidosRoute,
  novoOrderRoute,
  pedidoDetailRoute,
  pedidoPrintRoute,
  catalogoRoute,
  aprovacoes,
]);

export const router = createRouter({
  routeTree,
  defaultPreload: 'intent',
  defaultPreloadStaleTime: 0,
});

declare module '@tanstack/react-router' {
  interface Register {
    router: typeof router;
  }
}
