"use client";

import { usePathname } from "next/navigation";
import { api } from "@/lib/api-client";
import { useLocale } from "@/lib/i18n";
import DashboardPageLayout from "@/components/agnet/layout/DashboardPageLayout";
import {
  getActiveNavItem,
  getDashboardNavItems,
} from "@/components/agnet/layout/dashboard-nav";

interface Props {
  children: React.ReactNode;
  userPhone?: string;
  gisVerified?: boolean;
  subtitle?: string;
}

export default function FarmerDashboardShell({
  children,
  userPhone,
  gisVerified,
  subtitle,
}: Props) {
  const pathname = usePathname();
  const { locale } = useLocale();
  const navItems = getDashboardNavItems(locale);
  const activeItem = getActiveNavItem(pathname, navItems);

  const handleLogout = async () => {
    try {
      await api.auth.logout();
    } catch {
      /* best effort */
    }
    window.location.href = "/farmers/login";
  };

  return (
    <DashboardPageLayout
      navItems={navItems}
      title={activeItem.moduleTitle}
      subtitle={subtitle}
      userPhone={userPhone}
      gisVerified={gisVerified}
      onLogout={handleLogout}
    >
      {children}
    </DashboardPageLayout>
  );
}
