import { useApp } from "@/pages/app/AppContext";
import { BackHeader, Badge } from "@/pages/app/components/ui";
import {
  BIZ_OPP_STATUS,
  BizOpportunityRows,
  bizOpportunityTarget,
  fmt,
} from "@/pages/app/components/biz/BizUI";
import Countdown from "@/pages/app/components/bo/Countdown";
import { bizOpportunities } from "@/mocks/dshnBizOwner";

function ValueBox({ label, value }: { label: string; value: string }) {
  return (
    <div className="rounded-[12px] bg-background-200/70 p-2.5 min-w-0">
      <p className="text-[9.5px] text-foreground-600 leading-tight">{label}</p>
      <p className="text-[12.5px] font-bold text-foreground-950 mt-1 leading-tight truncate" dir="ltr">
        {value}
      </p>
    </div>
  );
}

/** Investment opportunities main page (section 2.7). */
export default function BizOpportunities() {
  const { goBack, navigate } = useApp();
  const current =
    bizOpportunities.find((o) => o.status === "published" || o.status === "review") ?? bizOpportunities[0];
  const previous = bizOpportunities.filter((o) => o.id !== current.id);
  const meta = BIZ_OPP_STATUS[current.status];
  const target = Date.now() + 51 * 86400000;
  const pct =
    current.coinvest && current.collected !== undefined && current.target
      ? Math.round((current.collected / current.target) * 100)
      : 0;

  return (
    <div className="min-h-full pb-8">
      <BackHeader title="فرص الاستثمار" onBack={goBack} />

      <div className="px-4 py-5 space-y-5">
        {/* Current opportunity */}
        <div>
          <h2 className="text-[15px] font-semibold text-foreground-950 mb-2.5">الفرصة الحالية</h2>
          <button
            type="button"
            onClick={() => navigate(bizOpportunityTarget(current))}
            className="w-full text-right rounded-[20px] bg-background-100 border border-background-200 p-4 cursor-pointer hover:border-background-300 transition-colors"
          >
            <div className="flex items-start justify-between gap-2">
              <div className="min-w-0">
                <p className="text-[14px] font-bold text-foreground-950">{current.category}</p>
                <p className="text-[11.5px] text-foreground-600 mt-0.5">مرحلة المشروع: {current.stage}</p>
                {current.coinvest && (
                  <span className="inline-flex items-center gap-1 mt-1.5 text-[10.5px] font-semibold text-accent-800">
                    <i className="ri-team-line text-[12px] leading-none" />
                    استثمار تشاركي
                  </span>
                )}
              </div>
              <Badge tone={meta.tone}>{meta.label}</Badge>
            </div>

            <div className="grid grid-cols-3 gap-2 mt-3">
              <ValueBox label="قيمة الاستثمار المطلوبة" value={`${fmt(current.investAmount)} ر.س`} />
              <ValueBox label="النسبة المعروضة" value={`${current.equity}%`} />
              <ValueBox label="تقييم المنشأة" value={`${fmt(current.valuation)} ر.س`} />
            </div>

            {current.coinvest && current.collected !== undefined && current.target !== undefined && (
              <div className="mt-3">
                <div className="flex items-center justify-between text-[11px] mb-1.5">
                  <span className="text-foreground-600">تم جمع</span>
                  <span className="font-semibold text-foreground-900">
                    {fmt(current.collected)} / {fmt(current.target)} ر.س
                  </span>
                </div>
                <div className="h-2 rounded-full bg-background-200 overflow-hidden">
                  <div className="h-full bg-accent-500 rounded-full" style={{ width: `${pct}%` }} />
                </div>
              </div>
            )}

            {current.endDate && (
              <div className="mt-3 rounded-[14px] bg-secondary-50 border border-secondary-200 p-3">
                <p className="text-[11px] text-foreground-600 mb-2">تنتهي الفرصة خلال</p>
                <Countdown target={target} />
              </div>
            )}
          </button>
        </div>

        {/* Previous opportunities */}
        <div>
          <h2 className="text-[15px] font-semibold text-foreground-950 mb-2.5">الفرص السابقة</h2>
          <BizOpportunityRows items={previous} onOpen={(op) => navigate(bizOpportunityTarget(op))} />
        </div>
      </div>
    </div>
  );
}