import { useApp } from "@/pages/app/AppContext";
import { Badge, BackHeader, cn } from "@/pages/app/components/ui";
import { investorRequestDetails, type InvestorRequestDetailsData } from "@/mocks/dshnInvestor";

export type RequestStatus =
  | "new"
  | "talking"
  | "negotiating"
  | "agreed"
  | "rejected"
  | "cancelled";

export interface RequestDetailsOverride {
  data?: InvestorRequestDetailsData;
  infoLine?: string;
}

type Tone = "success" | "warn" | "danger" | "info" | "neutral";

const STATUS_META: Record<RequestStatus, { label: string; tone: Tone }> = {
  new: { label: "طلب جديد", tone: "info" },
  talking: { label: "جاري التواصل", tone: "warn" },
  negotiating: { label: "تفاوض", tone: "warn" },
  agreed: { label: "تم الاتفاق", tone: "success" },
  rejected: { label: "مرفوضة", tone: "danger" },
  cancelled: { label: "ملغاة", tone: "neutral" },
};

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

/**
 * Investor offer/request details — shared by every deal-status page.
 * The allowed action buttons change with the status; there is no bottom bar
 * when the status allows no action.
 */
export default function RequestDetails({
  status,
  override,
}: {
  status: RequestStatus;
  override?: RequestDetailsOverride;
}) {
  const { goBack, navigate } = useApp();
  const d = override?.data ?? investorRequestDetails;
  const meta = STATUS_META[status];

  const canChat = status === "talking" || status === "negotiating";
  const canCancel = status === "new" || status === "talking" || status === "negotiating";

  const rows = [
    { label: "قيمة الاستثمار", value: `${fmt(d.amount)} ر.س` },
    { label: "النسبة المطلوبة", value: `${d.equity}%` },
    { label: "تاريخ التقديم", value: d.date },
  ];

  return (
    <div className="min-h-full flex flex-col bg-background-50">
      <BackHeader title="تفاصيل الطلب" onBack={goBack} />

      <div className="flex-1 px-4 pt-5 pb-6 space-y-4">
        <button
          type="button"
          onClick={() => navigate("invBusiness", { businessId: d.businessId })}
          className="w-full rounded-[20px] bg-background-100 border border-background-200 p-4 flex items-center gap-3 text-right cursor-pointer transition-colors hover:border-background-300"
        >
          <div className="w-12 h-12 rounded-full overflow-hidden bg-background-200 shrink-0">
            <img src={d.businessLogo} alt={d.businessName} className="w-full h-full object-cover object-top" />
          </div>
          <div className="flex-1 min-w-0">
            <p className="text-[10.5px] text-foreground-600">المنشأة</p>
            <h3 className="text-[14.5px] font-semibold text-foreground-950 truncate mt-0.5">{d.businessName}</h3>
          </div>
          <i className="ri-arrow-left-s-line text-[20px] text-foreground-400 leading-none shrink-0" />
        </button>

        <div className="rounded-[20px] bg-background-100 border border-background-200 p-4 flex items-center justify-between gap-3">
          <span className="text-[13px] text-foreground-700">الحالة</span>
          <Badge tone={meta.tone}>{meta.label}</Badge>
        </div>

        <div className="rounded-[20px] bg-background-100 border border-background-200 px-4">
          {rows.map((r, i) => (
            <div
              key={r.label}
              className={cn(
                "flex items-center justify-between gap-3 py-3.5",
                i < rows.length - 1 && "border-b border-background-200/70",
              )}
            >
              <span className="text-[13px] text-foreground-600">{r.label}</span>
              <span className="text-[13.5px] font-semibold text-foreground-950">{r.value}</span>
            </div>
          ))}
        </div>

        {override?.infoLine && (
          <div className="flex items-center gap-2 rounded-[14px] bg-accent-100 px-3.5 py-3">
            <i className="ri-checkbox-circle-line text-[17px] text-accent-700 leading-none" />
            <span className="text-[12.5px] font-medium text-accent-900">{override.infoLine}</span>
          </div>
        )}

        {(canChat || canCancel) && (
          <div className="flex flex-col gap-3">
            {canChat && (
              <button
                type="button"
                onClick={() => navigate("invChat")}
                className="w-full h-[52px] rounded-[14px] bg-primary-500 text-background-50 text-[15px] font-semibold flex items-center justify-center gap-2 cursor-pointer active:scale-[0.98] transition-transform whitespace-nowrap"
              >
                <i className="ri-chat-3-line text-[18px] leading-none" />
                فتح المحادثة
              </button>
            )}
            {canCancel && (
              <button
                type="button"
                onClick={() => navigate("invRequestCancel")}
                className="w-full h-[52px] rounded-[14px] 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 whitespace-nowrap"
              >
                <i className="ri-close-circle-line text-[17px] leading-none" />
                إلغاء العرض
              </button>
            )}
          </div>
        )}
      </div>
    </div>
  );
}