import { createFileRoute, useNavigate } from '@tanstack/react-router';
import { z } from 'zod';
import { PageLayout } from '@/components/shared';
import { Database } from 'lucide-react';
import { TenantPage } from '@/features/tenants/TenantPage';
import { CPZone } from '@archer/domain';
import { requireZone } from '@/presentation/guards/requireZone';

const knowledgeSearchSchema = z.object({
  tenantId: z.string().uuid().optional().catch(undefined),
});

function KnowledgeRoute() {
  const { tenantId } = Route.useSearch();
  const navigate = useNavigate();

  return (
    <PageLayout
      title="Knowledge Base"
      subtitle="Manage your AI assistant's knowledge"
      icon={<Database size={24} />}
      gradient={true}
    >
      <TenantPage
        tenantId={tenantId}
        variant="knowledge"
        onTenantChange={(next) =>
          navigate({ to: '/dashboard/knowledge', search: { tenantId: next }, replace: true })
        }
      />
    </PageLayout>
  );
}

export const Route = createFileRoute('/_authenticated/dashboard/knowledge')({
  validateSearch: knowledgeSearchSchema,
  beforeLoad: () => requireZone(CPZone.KNOWLEDGE),
  component: KnowledgeRoute,
});
