import { useWeb } from "../WebContext";
import BoChrome from "./bo/BoChrome";
import { BoRegister, BoOtp, BoStatus, BoSetup } from "./bo/BoAuthSetup";
import { BoLogin } from "./bo/BoLogin";
import { BoOverview, BoBusiness, BoProducts, BoOpportunities } from "./bo/BoSectionsA";
import {
  BoInvestRequests,
  BoPartnerships,
  BoEarnings,
  BoCommission,
  BoNotifications,
  BoProfile,
} from "./bo/BoSectionsB";
import { BoOrdersSegment, BoOrderDetail, BoNegotiationDetail, BoNegotiationChat, BoInvoiceForm } from "./bo/BoOrdersPages";
import { BoProductForm, BoProductDetail } from "./bo/BoProductPages";
import { BoChatsPane } from "./bo/BoChatsPane";
import { BoVerify } from "./bo/BoManagePages";
import { BoOpportunityForm, BoOpportunityPage, BoCoinvest } from "./bo/BoOpportunityPages";
import { BoInvestDetail, BoInvestCounter, BoInvestChatPage, BoDealRecord, BoDealRecorded } from "./bo/BoInvestPages";
import {
  BoExplore,
  BoPartnerPage,
  BoPartnerChatPage,
  BoPartnerTerms,
  BoPartnerInvoice,
  BoPartnerReview,
  BoPartnerPay,
  BoPartnerCancel,
  BoPartnershipDetail,
} from "./bo/BoPartnerPages";

interface Meta {
  title: string;
  crumbs?: { label: string; route?: string }[];
}

const META: Record<string, Meta> = {
  home: { title: "الرئيسية" },
  orders: { title: "الطلبات" },
  products: { title: "المنتجات" },
  chats: { title: "المحادثات" },
  manage: { title: "إدارة المنشأة" },
  opportunities: { title: "فرص الاستثمار" },
  investRequests: { title: "طلبات الاستثمار" },
  explore: { title: "استكشاف المنشآت للشراكة" },
  partnerships: { title: "الشراكات" },
  earnings: { title: "الأرباح والسحب" },
  commissions: { title: "تسوية عمولات الاستثمار" },
  notifications: { title: "الإشعارات" },
  profile: { title: "حسابي" },
};

const SUB: Record<string, Meta> = {
  orderDetail: { title: "تفاصيل الطلب", crumbs: [{ label: "الطلبات", route: "biz-orders" }, { label: "تفاصيل الطلب" }] },
  negotiationDetail: { title: "طلب التفاوض", crumbs: [{ label: "الطلبات", route: "biz-orders-negotiations" }, { label: "طلب تفاوض" }] },
  negotiationChat: { title: "محادثة التفاوض", crumbs: [{ label: "الطلبات", route: "biz-orders-negotiations" }, { label: "المحادثة" }] },
  invoiceForm: { title: "رفع فاتورة", crumbs: [{ label: "الطلبات", route: "biz-orders-negotiations" }, { label: "فاتورة" }] },
  productForm: { title: "منتج جديد", crumbs: [{ label: "المنتجات", route: "biz-products" }, { label: "منتج جديد" }] },
  productDetail: { title: "تفاصيل المنتج", crumbs: [{ label: "المنتجات", route: "biz-products" }, { label: "تفاصيل المنتج" }] },
  manageVerify: { title: "التوثيق التجاري", crumbs: [{ label: "إدارة المنشأة", route: "biz-manage" }, { label: "التوثيق التجاري" }] },
  opportunityForm: { title: "إنشاء فرصة", crumbs: [{ label: "فرص الاستثمار", route: "biz-opportunities" }, { label: "إنشاء فرصة" }] },
  opportunityDetail: { title: "تفاصيل الفرصة", crumbs: [{ label: "فرص الاستثمار", route: "biz-opportunities" }, { label: "تفاصيل الفرصة" }] },
  coinvest: { title: "الاستثمار التشاركي", crumbs: [{ label: "فرص الاستثمار", route: "biz-opportunities" }, { label: "الاستثمار التشاركي" }] },
  investRequestDetail: { title: "تفاصيل طلب الاستثمار", crumbs: [{ label: "طلبات الاستثمار", route: "biz-invest-requests" }, { label: "التفاصيل" }] },
  investCounter: { title: "عرض مقابل", crumbs: [{ label: "طلبات الاستثمار", route: "biz-invest-requests" }, { label: "عرض مقابل" }] },
  investChat: { title: "محادثة الاستثمار", crumbs: [{ label: "طلبات الاستثمار", route: "biz-invest-requests" }, { label: "المحادثة" }] },
  dealRecord: { title: "تسجيل الصفقة", crumbs: [{ label: "طلبات الاستثمار", route: "biz-invest-requests" }, { label: "تسجيل الصفقة" }] },
  dealRecorded: { title: "تم تسجيل الصفقة", crumbs: [{ label: "طلبات الاستثمار", route: "biz-invest-requests" }, { label: "تم تسجيل الصفقة" }] },
  partnerPage: { title: "صفحة المنشأة", crumbs: [{ label: "استكشاف المنشآت", route: "biz-partners-explore" }, { label: "المنشأة" }] },
  partnerChat: { title: "محادثة الشراكة", crumbs: [{ label: "الشراكات", route: "biz-partnerships" }, { label: "المحادثة" }] },
  partnershipDetail: { title: "تفاصيل الشراكة", crumbs: [{ label: "الشراكات", route: "biz-partnerships" }, { label: "التفاصيل" }] },
  partnerTerms: { title: "بنود الشراكة", crumbs: [{ label: "الشراكات", route: "biz-partnerships" }, { label: "البنود" }] },
  partnerInvoice: { title: "فاتورة الشراكة", crumbs: [{ label: "الشراكات", route: "biz-partnerships" }, { label: "فاتورة" }] },
  partnerReview: { title: "مراجعة الشراكة", crumbs: [{ label: "الشراكات", route: "biz-partnerships" }, { label: "مراجعة" }] },
  partnerPay: { title: "دفع الشراكة", crumbs: [{ label: "الشراكات", route: "biz-partnerships" }, { label: "دفع" }] },
  partnerCancel: { title: "إلغاء الشراكة", crumbs: [{ label: "الشراكات", route: "biz-partnerships" }, { label: "إلغاء" }] },
};

const PARTNER_VIEWS = [
  "partnerChat",
  "partnerTerms",
  "partnerInvoice",
  "partnerReview",
  "partnerPay",
  "partnerCancel",
  "partnershipDetail",
];

export default function BizOwnerWeb() {
  const { params, navigate } = useWeb();
  const view = params.view;
  const section = params.section ?? (view ? view : "home");

  // Auth + setup render as bare centered pages (no dashboard chrome).
  if (section === "register") return <BoRegister variant={params.variant} />;
  if (section === "login" || section === "login-error")
    return <BoLogin variant={section === "login-error" ? "error" : undefined} />;
  if (section === "otp") return <BoOtp variant={params.variant} />;
  if (section === "pending") return <BoStatus state="pending" />;
  if (section === "rejected") return <BoStatus state="rejected" />;
  if (section === "setup")
    return <BoSetup step={view ?? params.bizRoute?.replace("biz-setup-", "")} />;

  const goSection = (s: string) => navigate("biz", { section: s });

  const navKey = section;
  const meta = view ? SUB[view] ?? META[section] : META[section] ?? { title: "لوحة التحكم" };

  const renderHome = () => <BoOverview onSection={goSection} />;

  return (
    <BoChrome activeKey={navKey} title={meta.title} crumbs={meta.crumbs}>
      {section === "home" && renderHome()}

      {section === "orders" && view === "orderDetail" && <BoOrderDetail />}
      {section === "orders" && view === "negotiationDetail" && (
        <BoNegotiationDetail onOpenChat={() => navigate("biz", { bizRoute: "biz-negotiation-chat", id: params.id })} />
      )}
      {section === "orders" && view === "negotiationChat" && <BoNegotiationChat />}
      {section === "orders" && view === "invoiceForm" && <BoInvoiceForm />}
      {section === "orders" && !view && <BoOrdersSegment />}

      {section === "products" && view === "productForm" && <BoProductForm key={params.id ?? "new"} />}
      {section === "products" && view === "productDetail" && <BoProductDetail key={params.id ?? "first"} />}
      {section === "products" && !view && <BoProducts />}

      {section === "chats" && <BoChatsPane />}

      {section === "manage" && view === "manageVerify" && <BoVerify />}
      {section === "manage" && !view && <BoBusiness />}

      {section === "opportunities" && view === "opportunityForm" && <BoOpportunityForm />}
      {section === "opportunities" && view === "opportunityDetail" && <BoOpportunityPage />}
      {section === "opportunities" && view === "coinvest" && <BoCoinvest />}
      {section === "opportunities" && !view && <BoOpportunities />}

      {section === "investRequests" && view === "investRequestDetail" && <BoInvestDetail />}
      {section === "investRequests" && view === "investCounter" && <BoInvestCounter />}
      {section === "investRequests" && view === "investChat" && <BoInvestChatPage />}
      {section === "investRequests" && view === "dealRecord" && <BoDealRecord key={params.id ?? "first"} />}
      {section === "investRequests" && view === "dealRecorded" && <BoDealRecorded />}
      {section === "investRequests" && !view && <BoInvestRequests />}

      {section === "explore" && view === "partnerPage" && <BoPartnerPage />}
      {section === "explore" && view !== "partnerPage" && <BoExplore />}

      {section === "partnerships" && view === "partnerChat" && <BoPartnerChatPage />}
      {section === "partnerships" && view === "partnerTerms" && <BoPartnerTerms />}
      {section === "partnerships" && view === "partnerInvoice" && <BoPartnerInvoice />}
      {section === "partnerships" && view === "partnerReview" && <BoPartnerReview />}
      {section === "partnerships" && view === "partnerPay" && <BoPartnerPay />}
      {section === "partnerships" && view === "partnerCancel" && <BoPartnerCancel />}
      {section === "partnerships" && view === "partnershipDetail" && <BoPartnershipDetail />}
      {section === "partnerships" && !PARTNER_VIEWS.includes(view ?? "") && <BoPartnerships />}

      {section === "earnings" && <BoEarnings openWithdraw={view === "withdraw"} />}
      {section === "commissions" && <BoCommission openSettlement={view === "settlement"} />}
      {section === "notifications" && <BoNotifications />}
      {section === "profile" && <BoProfile />}
    </BoChrome>
  );
}