"use client";

import { useState } from "react";
import { useRouter } from "next/navigation";
import { useTranslations } from "next-intl";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { LanguageSelect } from "@/components/LanguageSelect";
import { ThemeSelect } from "@/components/ThemeSelect";
import {
  User,
  Lock,
  Eye,
  EyeOff,
  ArrowRight
} from "lucide-react";

export function LoginPage() {
  const router = useRouter();
  const t = useTranslations('auth');
  const tCommon = useTranslations('common');
  const tValidation = useTranslations('validation');
  const [userId, setUserId] = useState("");
  const [password, setPassword] = useState("");
  const [showPassword, setShowPassword] = useState(false);
  const [rememberMe, setRememberMe] = useState(false);
  const [error, setError] = useState("");

  const handleLogin = async () => {
    setError("");

    // Validation
    if (!userId || !password) {
      setError(tValidation('required'));
      return;
    }

    try {
      // ✅ HttpOnly Cookie 방식: Next.js API Route로 프록시
      // 토큰은 JavaScript에서 접근 불가능한 HttpOnly 쿠키로 저장됨
      const response = await fetch('/api/auth/login', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          user_id: userId,
          user_pwd: password,
        }),
      });

      const data = await response.json();

      if (!response.ok) {
        throw {
          status: response.status,
          message: data.error || 'Login failed',
        };
      }

      console.log('✅ 로그인 성공:', data.message);
      console.log('📦 사용자 정보:', data.user);
      console.log('🔐 토큰은 안전한 HttpOnly 쿠키에 저장됨 (JavaScript 접근 불가)');

      // 대시보드로 이동
      router.push("/dashboard");
    } catch (err: any) {
      console.error('❌ 로그인 실패:', err);

      if (err.status === 422) {
        setError(t('invalidCredentials'));
      } else if (err.status === 429) {
        setError('너무 많은 로그인 시도가 있었습니다. 잠시 후 다시 시도해주세요.');
      } else {
        setError(err.message || t('invalidCredentials'));
      }
    }
  };


  return (
    <div className="min-h-screen bg-background flex flex-col">
      {/* Header */}
      <header className="clean-glass border-b border-border">
        <div className="container mx-auto px-6 py-4">
          <div className="flex items-center justify-between">
            <button
              onClick={() => router.push("/")}
              className="flex items-center space-x-3 hover:opacity-80 transition-opacity"
            >
              <div className="w-10 h-10 rounded-xl flex items-center justify-center clean-shadow relative overflow-hidden" style={{ backgroundColor: '#3B82F6' }}>
                <div className="text-white font-bold text-lg">S</div>
                <div className="absolute inset-0 bg-gradient-to-br from-white/10 to-transparent opacity-30"></div>
              </div>
              <div>
                <h1 className="text-xl font-bold tracking-wide">SAM</h1>
                <p className="text-xs text-muted-foreground">{t('login')}</p>
              </div>
            </button>
            <div className="flex items-center gap-3">
              <ThemeSelect />
              <LanguageSelect />
              <Button variant="ghost" onClick={() => router.push("/signup")} className="rounded-xl">
                {t('signUp')}
              </Button>
            </div>
          </div>
        </div>
      </header>

      {/* Main Content */}
      <div className="flex-1 flex items-center justify-center px-6 py-12">
        <div className="w-full max-w-md">
          {/* Login Card */}
          <div className="clean-glass rounded-2xl p-8 clean-shadow space-y-6">
            <div className="text-center">
              <h2 className="mb-2 text-foreground">{t('login')}</h2>
              <p className="text-muted-foreground">{tCommon('welcome')} SAM MES</p>
            </div>

            {error && (
              <div className="bg-destructive/10 border border-destructive/30 rounded-xl p-4">
                <p className="text-sm text-destructive text-center">{error}</p>
              </div>
            )}

            <div className="space-y-4">
              <div>
                <Label htmlFor="userId" className="flex items-center space-x-2 mb-2">
                  <User className="w-4 h-4" />
                  <span>{t('userId')}</span>
                </Label>
                <Input
                  id="userId"
                  type="text"
                  placeholder={t('userIdPlaceholder')}
                  value={userId}
                  onChange={(e) => setUserId(e.target.value)}
                  onKeyDown={(e) => e.key === "Enter" && handleLogin()}
                  className="clean-input"
                />
              </div>

              <div>
                <Label htmlFor="password" className="flex items-center space-x-2 mb-2">
                  <Lock className="w-4 h-4" />
                  <span>{t('password')}</span>
                </Label>
                <div className="relative">
                  <Input
                    id="password"
                    type={showPassword ? "text" : "password"}
                    placeholder={t('passwordPlaceholder')}
                    value={password}
                    onChange={(e) => setPassword(e.target.value)}
                    onKeyDown={(e) => e.key === "Enter" && handleLogin()}
                    className="clean-input pr-10"
                  />
                  <button
                    type="button"
                    onClick={() => setShowPassword(!showPassword)}
                    className="absolute right-3 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground"
                  >
                    {showPassword ? (
                      <EyeOff className="w-5 h-5" />
                    ) : (
                      <Eye className="w-5 h-5" />
                    )}
                  </button>
                </div>
              </div>

              <div className="flex items-center justify-between">
                <label className="flex items-center space-x-2 cursor-pointer">
                  <input
                    type="checkbox"
                    checked={rememberMe}
                    onChange={(e) => setRememberMe(e.target.checked)}
                    className="w-4 h-4 rounded border-border"
                  />
                  <span className="text-sm text-muted-foreground">{t('rememberMe')}</span>
                </label>
                <button className="text-sm text-primary hover:underline">
                  {t('forgotPassword')}
                </button>
              </div>
            </div>

            <Button
              onClick={handleLogin}
              className="w-full rounded-xl bg-primary hover:bg-primary/90"
            >
              {t('login')}
              <ArrowRight className="ml-2 w-4 h-4" />
            </Button>

            <div className="relative">
              <div className="absolute inset-0 flex items-center">
                <div className="w-full border-t border-border"></div>
              </div>
              <div className="relative flex justify-center text-sm">
                <span className="px-4 bg-card text-muted-foreground">{tCommon('or')}</span>
              </div>
            </div>

            <div className="space-y-3">
              <Button
                variant="outline"
                onClick={() => router.push("/signup")}
                className="w-full rounded-xl"
              >
                {t('createAccount')}
              </Button>
            </div>
          </div>

          {/* Signup Link */}
          <div className="text-center mt-6">
            <p className="text-sm text-muted-foreground">
              {t('noAccount')}{" "}
              <button
                onClick={() => router.push("/signup")}
                className="text-primary font-medium hover:underline"
              >
                {t('signUp')}
              </button>
            </p>
          </div>
        </div>
      </div>
    </div>
  );
}