import { useApp } from "@/pages/app/AppContext";
import { Badge, cn } from "@/pages/app/components/ui";
import { investorPhoneDisplay, investorProfile } from "@/mocks/dshnInvestor";

function fmt(n: number) {
  return n.toLocaleString("en-US");
}

const ROW = "w-full h-[60px] px-4 flex items-center gap-3.5 cursor-pointer hover:bg-background-50 transition-colors";

const ICON_WRAP = "w-10 h-10 rounded-[12px] bg-background-200 flex items-center justify-center shrink-0";
const ICON = "text-[19px] text-foreground-950 leading-none";
const LABEL = "flex-1 text-right text-[14px] font-medium text-foreground-950 truncate";
const CHEVRON = "ri-arrow-left-s-line text-[20px] text-foreground-400 leading-none shrink-0";
const VALUE = "text-[12.5px] text-foreground-500 shrink-0";

/**
 * Investor "حسابي" (profile) page body — normal page flow (no fixed).
 *
 * Order:
 *  1) sticky top bar
 *  2) profile card (avatar + name)
 *  3) combined card: personal data + investment profile (read-only) with "تعديل"
 *  4) grouped options list: تعديل البيانات / تغيير رقم الجوال / تغيير اللغة / الإشعارات
 *  5) logout button
 *  6) delete-account (red-tint row, last item)
 *
 * `pending` shows the yellow "under review" badges (separate state page).
 * `notifOn` reflects the notification toggle (ON = black).
 */
export default function InvProfileBody({
  onLogout,
  pending = false,
  notifOn = true,
}: {
  onLogout: () => void;
  pending?: boolean;
  notifOn?: boolean;
}) {
  const { navigate } = useApp();

  return (
    <>
      {/* Sticky top bar — first element in flow */}
      <header className="sticky top-0 z-30 shrink-0 bg-background-50/95 backdrop-blur border-b border-background-200/70">
        <div className="h-[56px] px-4 flex items-center">
          <h1 className="text-[18px] font-bold text-foreground-950">حسابي</h1>
        </div>
      </header>

      {/* Page content — bottom padding = bottom nav height + 24px */}
      <div className="px-4 pt-5 pb-[104px]">
        {/* Profile card — avatar + name only */}
        <div className="rounded-[20px] bg-background-100 border border-background-200 p-5 flex items-center gap-4">
          <div className="w-[72px] h-[72px] rounded-full overflow-hidden bg-background-200 shrink-0">
            <img
              src={investorProfile.avatar}
              alt={investorProfile.name}
              className="w-full h-full object-cover object-top"
            />
          </div>
          <h2 className="flex-1 min-w-0 text-[17px] font-bold text-foreground-950 truncate">
            {investorProfile.name}
          </h2>
        </div>

        {/* Combined card — personal data + investment profile, header has the "تعديل" link */}
        <div className="mt-4 rounded-[20px] bg-background-100 border border-background-200 p-4">
          <div className="flex items-center justify-between gap-3">
            <h3 className="text-[14px] font-bold text-foreground-950 shrink-0">بياناتك وملفك الاستثماري</h3>
            <div className="flex items-center gap-2 min-w-0">
              {pending && <Badge tone="warn">تعديل قيد المراجعة</Badge>}
              <button
                type="button"
                onClick={() => navigate("invEditData")}
                className="text-[12.5px] font-semibold text-accent-700 cursor-pointer whitespace-nowrap shrink-0"
              >
                تعديل
              </button>
            </div>
          </div>

          {/* Section — personal data */}
          <h4 className="mt-3.5 mb-2 text-[12px] font-semibold text-foreground-500">البيانات الشخصية</h4>
          <div className="space-y-3.5">
            <div className="flex items-center justify-between gap-3">
              <span className="text-[12.5px] text-foreground-600">الاسم الكامل</span>
              <span className="text-[13px] font-semibold text-foreground-950 text-left truncate">
                {investorProfile.name}
              </span>
            </div>
            <div className="h-px bg-background-200" />
            <div className="flex items-center justify-between gap-3">
              <span className="text-[12.5px] text-foreground-600">المدينة</span>
              <span className="text-[13px] font-semibold text-foreground-950">{investorProfile.city}</span>
            </div>
            <div className="h-px bg-background-200" />
            <div className="flex items-center justify-between gap-3">
              <span className="text-[12.5px] text-foreground-600">الدولة</span>
              <span className="text-[13px] font-semibold text-foreground-950">{investorProfile.country}</span>
            </div>
          </div>

          <div className="h-px bg-background-200 my-4" />

          {/* Section — investment profile */}
          <h4 className="mb-2 text-[12px] font-semibold text-foreground-500">الملف الاستثماري</h4>
          <div className="space-y-3.5">
            <div className="flex items-center justify-between gap-3">
              <span className="text-[12.5px] text-foreground-600">نوع المستثمر</span>
              <span className="text-[13px] font-semibold text-foreground-950">{investorProfile.typeLabel}</span>
            </div>
            <div className="h-px bg-background-200" />
            <div>
              <span className="text-[12.5px] text-foreground-600">القطاعات المهتم بها</span>
              <div className="flex flex-wrap gap-1.5 mt-2">
                {investorProfile.sectors.map((s) => (
                  <span
                    key={s}
                    className="rounded-full bg-accent-100 text-accent-800 px-2.5 py-1 text-[11px] font-medium"
                  >
                    {s}
                  </span>
                ))}
              </div>
            </div>
            <div className="h-px bg-background-200" />
            <div className="flex items-center justify-between gap-3">
              <span className="text-[12.5px] text-foreground-600">نطاق الاستثمار</span>
              <span className="text-[13px] font-semibold text-foreground-950 whitespace-nowrap" dir="ltr">
                {fmt(investorProfile.minRange)} – {fmt(investorProfile.maxRange)} ر.س
              </span>
            </div>
          </div>
        </div>

        {/* Options list — one grouped card, 60px rows, hairline dividers */}
        <div className="mt-4 rounded-[20px] bg-background-100 border border-background-200 overflow-hidden divide-y divide-background-200">
          {/* 1) تعديل البيانات */}
          <button type="button" onClick={() => navigate("invEditData")} className={ROW}>
            <span className={ICON_WRAP}>
              <i className={cn("ri-user-settings-line", ICON)} />
            </span>
            <span className={LABEL}>تعديل البيانات</span>
            {pending && <Badge tone="warn">قيد المراجعة</Badge>}
            <i className={CHEVRON} />
          </button>

          {/* 2) تغيير رقم الجوال */}
          <button type="button" onClick={() => navigate("invPhoneOld")} className={ROW}>
            <span className={ICON_WRAP}>
              <i className={cn("ri-smartphone-line", ICON)} />
            </span>
            <span className={LABEL}>تغيير رقم الجوال</span>
            {pending && <Badge tone="warn">قيد المراجعة</Badge>}
            <span className={VALUE} dir="ltr">
              {investorPhoneDisplay}
            </span>
            <i className={CHEVRON} />
          </button>

          {/* 3) تغيير اللغة */}
          <button type="button" onClick={() => navigate("invLanguage")} className={ROW}>
            <span className={ICON_WRAP}>
              <i className={cn("ri-translate-2", ICON)} />
            </span>
            <span className={LABEL}>تغيير اللغة</span>
            <span className={VALUE}>العربية</span>
            <i className={CHEVRON} />
          </button>

          {/* 4) الإشعارات — ON/OFF toggle (black when on), no chevron */}
          <button
            type="button"
            onClick={() => navigate(notifOn ? "invProfileNotifOff" : "invProfile")}
            className={ROW}
          >
            <span className={ICON_WRAP}>
              <i className={cn("ri-notification-3-line", ICON)} />
            </span>
            <span className={LABEL}>الإشعارات</span>
            <span
              className={cn(
                "relative w-[48px] h-[28px] rounded-full shrink-0 transition-colors",
                notifOn ? "bg-foreground-950" : "bg-background-300",
              )}
              aria-hidden="true"
            >
              <span
                className={cn(
                  "absolute top-1 w-[20px] h-[20px] rounded-full bg-background-50 transition-all",
                  notifOn ? "right-1" : "right-[26px]",
                )}
              />
            </span>
          </button>
        </div>

        {/* Logout button — white bg, light red border, red text */}
        <button
          type="button"
          onClick={onLogout}
          className="mt-6 w-full h-[52px] rounded-[14px] bg-background-100 border border-danger-200 text-danger-600 text-[14.5px] font-semibold flex items-center justify-center gap-2 cursor-pointer hover:bg-danger-50 transition-colors whitespace-nowrap"
        >
          <i className="ri-logout-box-r-line text-[19px] leading-none" />
          تسجيل الخروج
        </button>

        {/* Delete account — red-tint row, last item */}
        <button
          type="button"
          onClick={() => navigate("invDeleteAccount")}
          className="mt-3 w-full h-[60px] px-4 rounded-[18px] bg-danger-50 border border-danger-200 flex items-center gap-3.5 cursor-pointer hover:bg-danger-100 transition-colors"
        >
          <span className="w-10 h-10 rounded-[12px] bg-danger-100 flex items-center justify-center shrink-0">
            <i className="ri-delete-bin-6-line text-[19px] text-danger-700 leading-none" />
          </span>
          <span className="flex-1 text-right text-[14px] font-semibold text-danger-700">حذف الحساب</span>
          <i className="ri-arrow-left-s-line text-[20px] text-danger-400 leading-none shrink-0" />
        </button>
      </div>
    </>
  );
}