"use client";

import { useState } from "react";
import { useRouter, useSearchParams } from "next/navigation";

import { LoginPanel } from "@/components/auth/login/LoginPanel";
import type { LoginTab, PasswordView } from "@/components/auth/login/types";
import { DESKTOP_ENTRY } from "@/lib/desktop-shell";
import { isSafeReturnTo } from "@/lib/safe-redirect";

/**
 * 桌面壳的唯一登录面：没有首页、没有弹窗、没有关闭。
 *
 * ![桌面登录](docs/prototype/desktop-login/openpencil/exports/desktop-login.png)
 */
const DesktopLoginPage = () => {
  const router = useRouter();
  const from = useSearchParams().get("from");
  const [activeTab, setActiveTab] = useState<LoginTab>("sms");
  const [passwordView, setPasswordView] = useState<PasswordView>("login");

  return (
    <main className="flex flex-1 items-center justify-center">
      <div className="home-paper w-115 rounded-3xl bg-surface shadow-card">
        <LoginPanel
          activeTab={activeTab}
          onTabChange={setActiveTab}
          passwordView={passwordView}
          onPasswordView={setPasswordView}
          onSuccess={() => router.replace(from && isSafeReturnTo(from) ? from : DESKTOP_ENTRY)}
        />
      </div>
    </main>
  );
};

export default DesktopLoginPage;
