import { useState } from "react";
import { useApp } from "@/pages/app/AppContext";
import { BackHeader, Btn } from "@/pages/app/components/ui";
import LocationMap, { type LatLng } from "@/pages/app/components/biz/LocationMap";
import { myBusiness } from "@/mocks/dshnBusiness";

export default function BizLocation() {
  const { goBack } = useApp();
  const [pos, setPos] = useState<LatLng>({ lat: myBusiness.mapLat, lng: myBusiness.mapLng });
  const [saved, setSaved] = useState(false);

  return (
    <div className="flex min-h-full flex-col bg-background-50">
      <BackHeader title="موقع المنشأة" subtitle="حدّد موقع منشأتك بدقة على الخريطة" onBack={goBack} />

      <div className="flex-1 space-y-4 px-4 py-5">
        <LocationMap
          value={pos}
          onChange={setPos}
          draggable
          height={340}
          hint="اسحب الدبوس أو اضغط على الخريطة لتحديد الموقع"
        />

        <div className="rounded-[20px] bg-background-100 border border-background-200 p-4">
          <div className="flex items-center gap-3">
            <div className="w-10 h-10 rounded-[12px] bg-accent-100 flex items-center justify-center shrink-0">
              <i className="ri-map-pin-2-line text-[19px] text-accent-800 leading-none" />
            </div>
            <div className="min-w-0">
              <p className="text-[12px] text-foreground-600">الموقع المحدد</p>
              <p className="text-[13.5px] font-semibold text-foreground-950">{myBusiness.mapAddress}</p>
            </div>
          </div>
          <div className="grid grid-cols-2 gap-2.5 mt-3">
            <div className="rounded-[12px] bg-background-200/70 px-3 py-2">
              <p className="text-[10.5px] text-foreground-600">خط العرض</p>
              <p className="text-[12.5px] font-semibold text-foreground-950" dir="ltr">
                {pos.lat.toFixed(5)}
              </p>
            </div>
            <div className="rounded-[12px] bg-background-200/70 px-3 py-2">
              <p className="text-[10.5px] text-foreground-600">خط الطول</p>
              <p className="text-[12.5px] font-semibold text-foreground-950" dir="ltr">
                {pos.lng.toFixed(5)}
              </p>
            </div>
          </div>
        </div>

        {saved && (
          <div className="flex items-center gap-2 rounded-[14px] bg-accent-50 border border-accent-200 px-3.5 py-3">
            <i className="ri-checkbox-circle-fill text-[17px] text-accent-700 leading-none" />
            <span className="text-[12px] text-foreground-700">تم حفظ موقع المنشأة بنجاح.</span>
          </div>
        )}
      </div>

      <div className="sticky bottom-0 z-30 px-5 py-5 bg-background-50 border-t border-background-200/70">
        <Btn
          full
          onClick={() => {
            setSaved(true);
            window.setTimeout(goBack, 650);
          }}
        >
          <i className="ri-map-pin-2-line text-[17px] leading-none" />
          تأكيد الموقع
        </Btn>
      </div>
    </div>
  );
}