"use client";

import { useState, useEffect } 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 { transformApiMenusToMenuItems } from "@/lib/utils/menuTransform";
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 [isChecking, setIsChecking] = useState(true);

  // 이미 로그인된 상태인지 확인 (페이지 로드 시, 뒤로가기 시)
  useEffect(() => {
    const checkAuth = async () => {
      try {
        // 🔵 Next.js 내부 API - 쿠키에서 토큰 확인 (PHP 호출 X, 성능 최적화)
        const response = await fetch('/api/auth/check');

        if (response.ok) {
          // 이미 로그인됨 → 대시보드로 리다이렉트 (replace로 히스토리에서 제거)
          router.replace('/dashboard');
          return;
        }
        // 인증 안됨 (401) → 현재 페이지 유지
      } catch {
        // API 호출 실패 → 현재 페이지 유지
      } finally {
        setIsChecking(false);
      }
    };

    checkAuth();
  }, [router]);

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

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

    try {
      // 🔵 Next.js 프록시 → PHP /api/v1/login (토큰을 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('📋 메뉴 정보 (API):', data.menus);
      console.log('👥 역할 정보:', data.roles);
      console.log('🏢 테넌트 정보:', data.tenant);
      console.log('🔐 토큰은 안전한 HttpOnly 쿠키에 저장됨 (JavaScript 접근 불가)');

      // API 메뉴를 MenuItem 구조로 변환
      const transformedMenus = transformApiMenusToMenuItems(data.menus || []);
      console.log('🔄 변환된 메뉴 구조:', transformedMenus);

      // 서버에서 받은 사용자 정보를 localStorage에 저장 (대시보드에서 사용)
      const userData = {
        name: data.user?.name || userId,
        position: data.roles?.[0]?.description || '사용자',
        userId: userId,
        menu: transformedMenus, // 변환된 메뉴 구조 저장
        roles: data.roles || [],
        tenant: data.tenant || {},
      };
      console.log('💾 localStorage에 저장할 데이터:', userData);
      localStorage.setItem('user', JSON.stringify(userData));

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

      const error = err as { status?: number; message?: string };

      if (error.status === 401 || error.status === 422) {
        setError(t('invalidCredentials'));
      } else if (error.status === 429) {
        setError('Too many login attempts. Please try again later.');
      } else if (error.status && error.status >= 500) {
        setError('Service temporarily unavailable. Please try again later.');
      } else {
        setError(error.message || t('invalidCredentials'));
      }
    }
  };


  // 인증 체크 중일 때는 로딩 표시
  if (isChecking) {
    return (
      <div className="min-h-screen flex items-center justify-center">
        <div className="text-center">
          <div className="inline-block h-8 w-8 animate-spin rounded-full border-4 border-solid border-primary border-r-transparent mb-4"></div>
          <p className="text-muted-foreground">Loading...</p>
        </div>
      </div>
    );
  }

  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 native={false} />
              <LanguageSelect native={false} />
              <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>
            )}

            <form onSubmit={(e) => { e.preventDefault(); handleLogin(); }} 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"
                  name="username"
                  type="text"
                  autoComplete="username"
                  placeholder={t('userIdPlaceholder')}
                  value={userId}
                  onChange={(e) => setUserId(e.target.value)}
                  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"
                    name="password"
                    type={showPassword ? "text" : "password"}
                    autoComplete="current-password"
                    placeholder={t('passwordPlaceholder')}
                    value={password}
                    onChange={(e) => setPassword(e.target.value)}
                    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 type="button" className="text-sm text-primary hover:underline">
                  {t('forgotPassword')}
                </button>
              </div>

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

            <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>
  );
}