import { notFound, redirect } from "next/navigation";
import { PatientLoginForm } from "@/components/PatientLoginForm";
import { getPatientSession } from "@/lib/auth";
import { getCaseByToken } from "@/lib/cases";
import { text } from "@/lib/i18n";

export const metadata = { title: "ورود بیمار" };

export default async function PatientLoginPage({ params }: { params: Promise<{ token: string }> }) {
  const { token } = await params;
  const record = getCaseByToken(token);
  if (!record) notFound();
  const session = await getPatientSession();
  if (session && session.mobileKey === record.mobile_key) redirect(`/p/${token}/track`);
  const copy = text(record.language);
  const dir = record.language === "en" ? "ltr" : "rtl";
  return (
    <div className="app" lang={record.language} dir={dir}>
      <div className="auth">
        <section className="card stack">
          <p className="domain">panel.iranhealthagency.com</p>
          <h1>{copy.brand}</h1>
          <p>{copy.portal}</p>
          <h2>{copy.loginTitle}</h2>
          <p className="hint">{copy.loginHint}</p>
          <PatientLoginForm token={token} lang={record.language} />
        </section>
      </div>
    </div>
  );
}
