"use client";

import Link from "next/link";
import { signIn } from "next-auth/react";
import { useSearchParams } from "next/navigation";
import { Suspense } from "react";

const allowedEmailDomain = process.env.NEXT_PUBLIC_ALLOWED_EMAIL_DOMAIN;
const showDevLogin =
  process.env.NEXT_PUBLIC_ENABLE_DEV_LOGIN === "true" ||
  process.env.NODE_ENV !== "production";

const ERROR_MESSAGES: Record<string, string> = {
  AccessDenied: allowedEmailDomain
    ? `Access denied. Only @${allowedEmailDomain} accounts are allowed.`
    : "Access denied. Your account is not allowed to sign in.",
  OAuthSignin: "Could not start the sign-in flow. Please try again.",
  OAuthCallback: "Sign-in failed. Please try again.",
  Default: "An error occurred during sign-in. Please try again.",
};

function LoginContent() {
  const searchParams = useSearchParams();
  const errorCode = searchParams.get("error");
  const errorMessage = errorCode
    ? ERROR_MESSAGES[errorCode] ?? ERROR_MESSAGES.Default
    : null;

  return (
    <div className="max-w-sm w-full mx-auto mt-24 border border-gray-200 rounded-xl p-8">
      {errorMessage && (
        <div className="rounded-lg bg-red-50 border border-red-200 px-4 py-3 text-sm text-red-700 mb-6">
          {errorMessage}
        </div>
      )}

      {/* Logo */}
      <div className="flex flex-col items-center">
        <div className="h-10 w-10 bg-gray-900 rounded-lg flex items-center justify-center">
          <span className="text-white text-sm font-bold">P</span>
        </div>
        <h1 className="text-xl font-semibold text-gray-900 mt-4">PingCAP Social Hub</h1>
        <p className="text-sm text-gray-500 mt-1 mb-6">Sign in to continue</p>
      </div>

      <button
        onClick={() => signIn("google", { callbackUrl: "/dashboard" })}
        className="w-full border border-gray-200 rounded-lg py-2.5 flex items-center justify-center gap-2.5 text-sm text-gray-700 hover:bg-gray-50 font-medium transition-colors"
      >
        <svg width="20" height="20" viewBox="0 0 24 24">
          <path
            d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92a5.06 5.06 0 0 1-2.2 3.32v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.1z"
            fill="#4285F4"
          />
          <path
            d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"
            fill="#34A853"
          />
          <path
            d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z"
            fill="#FBBC05"
          />
          <path
            d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z"
            fill="#EA4335"
          />
        </svg>
        Continue with Google
      </button>
      {allowedEmailDomain ? (
        <p className="text-xs text-gray-400 text-center mt-4">@{allowedEmailDomain} accounts only</p>
      ) : (
        <p className="text-xs text-gray-400 text-center mt-4">Use an approved account to sign in</p>
      )}

      {showDevLogin ? (
        <div className="mt-6 border-t border-gray-100 pt-4 text-center">
          <Link href="/dev/login" className="text-sm font-medium text-blue-600 hover:text-blue-700">
            Use development quick login
          </Link>
        </div>
      ) : null}
    </div>
  );
}

export default function LoginPage() {
  return (
    <Suspense fallback={<div className="max-w-sm w-full mx-auto mt-24 p-8 text-gray-400 text-sm">Loading...</div>}>
      <LoginContent />
    </Suspense>
  );
}
