import { useState } from "react";
import { useApp } from "@/pages/app/AppContext";
import { BackHeader, Btn } from "@/pages/app/components/ui";
import { Field, StepIndicator, TextInput } from "@/pages/app/components/bo/BoUI";
import { customerRegisteredPhone, customerTakenPhone } from "@/mocks/dshnProfile";

/** Step 3 of 4 — enter the NEW phone number. */
export default function ProfilePhoneNew() {
  const { goBack, navigate } = useApp();
  const [phone, setPhone] = useState("");
  const [error, setError] = useState<"" | "same" | "taken">("");

  const submit = () => {
    if (phone === customerRegisteredPhone) {
      setError("same");
      return;
    }
    if (phone === customerTakenPhone) {
      setError("taken");
      return;
    }
    navigate("profilePhoneNewOtp");
  };

  return (
    <div className="min-h-full flex flex-col bg-background-50">
      <BackHeader title="الرقم الجديد" onBack={goBack} />

      <div className="px-5 pt-4">
        <StepIndicator current={3} total={4} />
      </div>

      <div className="flex-1 px-5 pt-6">
        <p className="text-[13.5px] text-foreground-600 leading-relaxed">
          أدخل رقم جوالك الجديد الذي ترغب بربطه بحسابك
        </p>

        <div className="mt-5">
          <Field label="رقم الجوال الجديد" required>
            <TextInput
              value={phone}
              onChange={(v) => {
                setError("");
                setPhone(v.replace(/\D/g, "").slice(0, 9));
              }}
              placeholder="5XXXXXXXX"
              prefix="+966"
              dir="ltr"
            />
          </Field>
        </div>

        {error && (
          <div className="flex items-center gap-1.5 mt-3 text-danger-600">
            <i className="ri-error-warning-line text-[16px] leading-none" />
            <span className="text-[12.5px] font-medium">
              {error === "same" ? "الرقم الجديد مطابق للرقم الحالي" : "هذا الرقم مسجل بحساب آخر"}
            </span>
          </div>
        )}
      </div>

      <div className="px-5 py-5 pb-[calc(env(safe-area-inset-bottom)+1.25rem)]">
        <Btn full disabled={phone.length !== 9} onClick={submit}>
          إرسال كود التحقق
        </Btn>
      </div>
    </div>
  );
}