import { useState } from "react";
import { investorProfile, investorPending, investorPhoneDisplay } from "@/mocks/dshnInvestor";
import { useWeb } from "@/pages/web/WebContext";
import { Container, WCard, WBtn, StatusPill, cn, fmt } from "@/pages/web/WebUI";
import WebPageHeader from "@/pages/web/components/WebPageHeader";

export default function InvProfile() {
  const { jump, logout, lang, setLang, navigate } = useWeb();
  const [notifOn, setNotifOn] = useState(true);
  const [modal, setModal] = useState<"logout" | "delete" | "phone" | null>(null);

  return (
    <div className="relative animate-fade-in">
      <WebPageHeader title="حسابي" desc="بياناتك وملفك الاستثماري وإعدادات الحساب" crumb="حسابي" />

      <Container className="py-8">
        <div className="grid grid-cols-1 lg:grid-cols-3 gap-8 items-start">
          <div className="lg:col-span-2 space-y-6">
            {/* Identity */}
            <WCard className="p-6">
              <div className="flex items-center gap-5">
                <div className="w-[84px] h-[84px] 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>
                <div className="min-w-0">
                  <h1 className="text-[22px] font-bold text-foreground-950 font-heading truncate">{investorProfile.name}</h1>
                  <p className="text-[13.5px] text-foreground-600 mt-1" dir="ltr">{investorProfile.phone}</p>
                  <div className="mt-2 flex flex-wrap items-center gap-2">
                    <StatusPill tone="info" icon="ri-shield-user-line">مستثمر</StatusPill>
                    <StatusPill tone="success" icon="ri-checkbox-circle-line">موثّق</StatusPill>
                    {investorPending.data && <StatusPill tone="warn" icon="ri-time-line">تعديل بيانات قيد المراجعة</StatusPill>}
                  </div>
                </div>
                <WBtn variant="secondary" className="ms-auto hidden sm:inline-flex" icon="ri-edit-line" onClick={() => navigate("invProfileSetup")}>
                  تعديل البيانات
                </WBtn>
              </div>
            </WCard>

            {/* Investment profile */}
            <WCard className="p-6">
              <div className="flex items-center justify-between gap-3 mb-4">
                <h2 className="text-[17px] font-bold text-foreground-950">الملف الاستثماري</h2>
                {investorPending.investment && <StatusPill tone="warn" icon="ri-time-line">قيد المراجعة</StatusPill>}
              </div>
              <div className="grid grid-cols-1 sm:grid-cols-2 gap-5">
                <Info label="نوع المستثمر" value={investorProfile.typeLabel} />
                <Info label="الدولة" value={investorProfile.country} />
                <Info label="المدينة" value={investorProfile.city} />
                <Info label="نطاق الاستثمار" value={`${fmt(investorProfile.minRange)} - ${fmt(investorProfile.maxRange)} ر.س`} />
              </div>
              <div className="mt-5">
                <p className="text-[12.5px] font-semibold text-foreground-800 mb-2.5">القطاعات المهتم بها</p>
                <div className="flex flex-wrap gap-2">
                  {investorProfile.sectors.map((s) => (
                    <span key={s} className="rounded-full bg-secondary-100 text-secondary-900 px-3.5 py-1.5 text-[12px] font-medium">
                      {s}
                    </span>
                  ))}
                </div>
              </div>
              <div className="mt-5">
                <p className="text-[12.5px] font-semibold text-foreground-800 mb-2">نبذة</p>
                <p className="text-[13.5px] text-foreground-700 leading-relaxed">{investorProfile.bio}</p>
              </div>
            </WCard>

            {/* Phone */}
            <WCard className="p-6">
              <h2 className="text-[17px] font-bold text-foreground-950 mb-4">رقم الجوال</h2>
              <div className="flex items-center justify-between gap-3 rounded-[14px] bg-background-50 border border-background-200 p-4">
                <div>
                  <p className="text-[12px] text-foreground-600">الرقم الحالي</p>
                  <p className="text-[15px] font-bold text-foreground-950 mt-0.5" dir="ltr">{investorPhoneDisplay}</p>
                </div>
                <WBtn variant="secondary" size="sm" icon="ri-smartphone-line" onClick={() => setModal("phone")}>تغيير رقم الجوال</WBtn>
              </div>
            </WCard>
          </div>

          {/* Settings */}
          <div className=" space-y-6">
            <WCard className="p-5">
              <h3 className="text-[15px] font-bold text-foreground-950 mb-3">إعدادات الحساب</h3>
              <div className="space-y-1">
                <Row icon="ri-hand-coin-line" label="المتابعة" onClick={() => jump("invFollowing")} />
                <Row icon="ri-chat-3-line" label="المحادثات" onClick={() => jump("invChats")} />
                <Row icon="ri-notification-3-line" label="الإشعارات" onClick={() => jump("invNotifications")} />
                <Row icon="ri-edit-line" label="تعديل البيانات" onClick={() => navigate("invProfileSetup")} />
                <Row icon="ri-line-chart-line" label="تعديل الملف الاستثماري" onClick={() => navigate("invProfileSetup")} />
              </div>

              <div className="mt-4 pt-4 border-t border-background-200 flex items-center justify-between gap-3">
                <span className="text-[13.5px] font-medium text-foreground-800">تفعيل الإشعارات</span>
                <button
                  type="button"
                  onClick={() => setNotifOn((v) => !v)}
                  className={cn(
                    "w-12 h-6 rounded-full p-0.5 cursor-pointer transition-colors",
                    notifOn ? "bg-accent-700" : "bg-background-300",
                  )}
                >
                  <span
                    className={cn(
                      "block w-5 h-5 rounded-full bg-background-50 transition-transform",
                      notifOn ? "translate-x-6 rtl:-translate-x-6" : "translate-x-0",
                    )}
                  />
                </button>
              </div>

              <div className="mt-4 pt-4 border-t border-background-200">
                <p className="text-[12.5px] font-semibold text-foreground-800 mb-2.5">اللغة</p>
                <div className="flex items-center rounded-full bg-background-200 p-0.5 w-max text-[12.5px] font-semibold">
                  <button
                    type="button"
                    onClick={() => setLang("ar")}
                    className={cn("px-3.5 h-8 rounded-full cursor-pointer transition-colors", lang === "ar" ? "bg-primary-500 text-background-50" : "text-foreground-600")}
                  >
                    العربية
                  </button>
                  <button
                    type="button"
                    onClick={() => setLang("en")}
                    className={cn("px-3.5 h-8 rounded-full cursor-pointer transition-colors", lang === "en" ? "bg-primary-500 text-background-50" : "text-foreground-600")}
                  >
                    English
                  </button>
                </div>
              </div>

              <div className="mt-4 pt-4 border-t border-background-200 space-y-2">
                <button
                  type="button"
                  onClick={() => setModal("logout")}
                  className="w-full h-11 rounded-[12px] border border-background-200 bg-background-50 text-foreground-800 text-[14px] font-semibold flex items-center justify-center gap-2 cursor-pointer hover:bg-background-200"
                >
                  <i className="ri-logout-box-r-line text-[18px] leading-none" />
                  تسجيل الخروج
                </button>
                <button
                  type="button"
                  onClick={() => setModal("delete")}
                  className="w-full h-11 rounded-[12px] border border-danger-200 bg-danger-50 text-danger-700 text-[14px] font-semibold flex items-center justify-center gap-2 cursor-pointer hover:bg-danger-100"
                >
                  <i className="ri-delete-bin-line text-[18px] leading-none" />
                  حذف الحساب
                </button>
              </div>
            </WCard>
          </div>
        </div>
      </Container>

      {modal && (
        <div className="fixed inset-0 z-50 flex items-center justify-center p-4 bg-black/40">
          <div className="w-full max-w-[420px] bg-background-50 rounded-[20px] border border-background-200 p-6 text-center">
            <span
              className={cn(
                "w-14 h-14 rounded-full flex items-center justify-center mx-auto mb-4",
                modal === "delete" ? "bg-danger-100 text-danger-700" : modal === "phone" ? "bg-secondary-100 text-secondary-900" : "bg-background-200 text-foreground-700",
              )}
            >
              <i
                className={cn(
                  modal === "delete" ? "ri-delete-bin-line" : modal === "phone" ? "ri-smartphone-line" : "ri-logout-box-r-line",
                  "text-[26px] leading-none",
                )}
              />
            </span>
            <h3 className="text-[18px] font-bold text-foreground-950 font-heading">
              {modal === "delete" ? "هل تريد حذف حسابك؟" : modal === "phone" ? "تغيير رقم الجوال" : "تسجيل الخروج"}
            </h3>
            <p className="text-[13.5px] text-foreground-600 mt-2 leading-relaxed">
              {modal === "delete"
                ? "سيتم حذف حسابك وبياناتك نهائيًا ولا يمكن التراجع عن هذه الخطوة."
                : modal === "phone"
                  ? "سنرسل كود تحقق إلى رقمك الحالي ثم إلى الرقم الجديد. التغيير يمر بأربع خطوات ويُعتمد بعد التحقق."
                  : "سيتم إنهاء جلستك الحالية ويمكنك تسجيل الدخول مجددًا في أي وقت."}
            </p>
            <div className="flex gap-3 mt-6">
              <WBtn className="flex-1" variant="secondary" onClick={() => setModal(null)}>
                {modal === "phone" ? "رجوع" : "إلغاء"}
              </WBtn>
              <WBtn
                className="flex-1"
                variant={modal === "delete" ? "danger" : "primary"}
                onClick={() => {
                  if (modal === "phone") {
                    setModal(null);
                    return;
                  }
                  setModal(null);
                  logout();
                  jump("home");
                }}
              >
                {modal === "delete" ? "حذف" : modal === "phone" ? "بدء التغيير" : "تأكيد"}
              </WBtn>
            </div>
          </div>
        </div>
      )}
    </div>
  );
}

function Info({ label, value }: { label: string; value: string }) {
  return (
    <div className="rounded-[14px] bg-background-50 border border-background-200 p-4">
      <p className="text-[12px] text-foreground-600">{label}</p>
      <p className="text-[15px] font-bold text-foreground-950 mt-1">{value}</p>
    </div>
  );
}

function Row({ icon, label, onClick }: { icon: string; label: string; onClick: () => void }) {
  return (
    <button
      type="button"
      onClick={onClick}
      className="w-full h-11 px-3 rounded-[12px] flex items-center gap-2.5 text-[13.5px] font-medium text-foreground-800 hover:bg-background-200 cursor-pointer transition-colors"
    >
      <i className={cn(icon, "text-[18px] leading-none")} />
      {label}
      <i className="ri-arrow-left-s-line text-[18px] text-foreground-400 leading-none ms-auto" />
    </button>
  );
}