'use client'

import { useState, useEffect, useCallback, Suspense } from 'react'
import { useRouter, useSearchParams } from 'next/navigation'
import { useAuth } from '@/contexts/AuthContext'
import { ShieldCheck, AlertCircle, ArrowLeft, Mail, Lock, KeyRound } from 'lucide-react'
import Link from 'next/link'
import { api } from '@/lib/api'
import { USE_MOCK } from '@/contexts/AuthContext'

type LoginMode = 'password' | 'code'

function LoginForm() {
  const router = useRouter()
  const searchParams = useSearchParams()
  const { login } = useAuth()
  const [email, setEmail] = useState('')
  const [password, setPassword] = useState('')
  const [emailCode, setEmailCode] = useState('')
  const [loginMode, setLoginMode] = useState<LoginMode>('password')
  const [error, setError] = useState('')
  const [isLoading, setIsLoading] = useState(false)
  const [autoLoginAttempted, setAutoLoginAttempted] = useState(false)
  const [codeSending, setCodeSending] = useState(false)
  const [countdown, setCountdown] = useState(0)

  // 如果 URL 有 role 参数，自动触发 demo 登录
  const roleFromUrl = searchParams.get('role') as 'creator' | 'agency' | 'brand' | null

  // 倒计时
  useEffect(() => {
    if (countdown <= 0) return
    const timer = setTimeout(() => setCountdown(countdown - 1), 1000)
    return () => clearTimeout(timer)
  }, [countdown])

  const handleDemoLogin = async (role: 'creator' | 'agency' | 'brand') => {
    const emailMap = {
      creator: 'creator@demo.com',
      agency: 'agency@demo.com',
      brand: 'brand@demo.com',
    }
    const demoEmail = emailMap[role]
    setEmail(demoEmail)
    setPassword('demo123')
    setError('')
    setIsLoading(true)

    const result = await login({ email: demoEmail, password: 'demo123' })

    if (result.success) {
      switch (role) {
        case 'creator':
          router.push('/creator')
          break
        case 'agency':
          router.push('/agency')
          break
        case 'brand':
          router.push('/brand')
          break
      }
    } else {
      setError(result.error || '登录失败')
    }

    setIsLoading(false)
  }

  useEffect(() => {
    if (roleFromUrl && !isLoading && !autoLoginAttempted) {
      setAutoLoginAttempted(true)
      handleDemoLogin(roleFromUrl)
    }
  }, [roleFromUrl])

  const handleSendCode = useCallback(async () => {
    if (!email) {
      setError('请先输入邮箱')
      return
    }
    if (countdown > 0) return

    setError('')
    setCodeSending(true)

    try {
      if (USE_MOCK) {
        await new Promise((resolve) => setTimeout(resolve, 500))
        setCountdown(60)
        return
      }

      await api.sendEmailCode({ email, purpose: 'login' })
      setCountdown(60)
    } catch (err) {
      const error = err instanceof Error ? err.message : '发送验证码失败'
      setError(error)
    } finally {
      setCodeSending(false)
    }
  }, [email, countdown])

  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault()
    setError('')
    setIsLoading(true)

    const credentials = loginMode === 'code'
      ? { email, email_code: emailCode }
      : { email, password }

    const result = await login(credentials)

    if (result.success) {
      const stored = localStorage.getItem('miaosi_user')
      if (stored) {
        const user = JSON.parse(stored)
        switch (user.role) {
          case 'creator':
            router.push('/creator')
            break
          case 'agency':
            router.push('/agency')
            break
          case 'brand':
            router.push('/brand')
            break
          default:
            router.push('/')
        }
      }
    } else {
      setError(result.error || '登录失败')
    }

    setIsLoading(false)
  }

  return (
    <div className="min-h-screen bg-bg-page flex flex-col items-center justify-center px-6">
      <div className="w-full max-w-sm space-y-8">
        {/* 返回按钮 */}
        <Link
          href="/"
          className="inline-flex items-center gap-2 text-text-secondary hover:text-text-primary transition-colors"
        >
          <ArrowLeft className="w-4 h-4" />
          返回首页
        </Link>

        {/* Logo */}
        <div className="flex items-center gap-3">
          <div className="w-12 h-12 rounded-xl bg-gradient-to-br from-accent-indigo to-[#4F46E5] flex items-center justify-center shadow-[0px_8px_24px_-4px_rgba(99,102,241,0.4)]">
            <ShieldCheck className="w-7 h-7 text-white" />
          </div>
          <div>
            <span className="text-2xl font-bold text-text-primary">秒思</span>
            <p className="text-sm text-text-secondary">AI 智能审核平台</p>
          </div>
        </div>

        {/* 登录方式切换 */}
        <div className="flex bg-bg-elevated rounded-xl p-1 border border-border-subtle">
          <button
            type="button"
            onClick={() => { setLoginMode('password'); setError('') }}
            className={`flex-1 py-2.5 rounded-lg text-sm font-medium transition-all ${
              loginMode === 'password'
                ? 'bg-white text-text-primary shadow-sm'
                : 'text-text-tertiary hover:text-text-secondary'
            }`}
          >
            密码登录
          </button>
          <button
            type="button"
            onClick={() => { setLoginMode('code'); setError('') }}
            className={`flex-1 py-2.5 rounded-lg text-sm font-medium transition-all ${
              loginMode === 'code'
                ? 'bg-white text-text-primary shadow-sm'
                : 'text-text-tertiary hover:text-text-secondary'
            }`}
          >
            验证码登录
          </button>
        </div>

        {/* 登录表单 */}
        <form onSubmit={handleSubmit} className="space-y-5">
          {error && (
            <div className="flex items-center gap-2 p-3 bg-accent-coral/10 text-accent-coral rounded-lg text-sm">
              <AlertCircle size={16} />
              {error}
            </div>
          )}

          <div className="space-y-2">
            <label className="block text-sm font-medium text-text-primary">邮箱</label>
            <div className="relative">
              <Mail className="absolute left-4 top-1/2 -translate-y-1/2 w-5 h-5 text-text-tertiary" />
              <input
                type="email"
                placeholder="请输入邮箱"
                value={email}
                onChange={(e) => setEmail(e.target.value)}
                className="w-full pl-12 pr-4 py-3.5 bg-bg-elevated border border-border-subtle rounded-xl text-text-primary placeholder-text-tertiary focus:outline-none focus:ring-2 focus:ring-accent-indigo focus:border-transparent transition-all"
                required
              />
            </div>
          </div>

          {loginMode === 'password' ? (
            <div className="space-y-2">
              <label className="block text-sm font-medium text-text-primary">密码</label>
              <div className="relative">
                <Lock className="absolute left-4 top-1/2 -translate-y-1/2 w-5 h-5 text-text-tertiary" />
                <input
                  type="password"
                  placeholder="请输入密码"
                  value={password}
                  onChange={(e) => setPassword(e.target.value)}
                  className="w-full pl-12 pr-4 py-3.5 bg-bg-elevated border border-border-subtle rounded-xl text-text-primary placeholder-text-tertiary focus:outline-none focus:ring-2 focus:ring-accent-indigo focus:border-transparent transition-all"
                  required
                />
              </div>
            </div>
          ) : (
            <div className="space-y-2">
              <label className="block text-sm font-medium text-text-primary">验证码</label>
              <div className="flex gap-3">
                <div className="relative flex-1">
                  <KeyRound className="absolute left-4 top-1/2 -translate-y-1/2 w-5 h-5 text-text-tertiary" />
                  <input
                    type="text"
                    placeholder="请输入验证码"
                    value={emailCode}
                    onChange={(e) => setEmailCode(e.target.value.replace(/\D/g, '').slice(0, 6))}
                    maxLength={6}
                    className="w-full pl-12 pr-4 py-3.5 bg-bg-elevated border border-border-subtle rounded-xl text-text-primary placeholder-text-tertiary focus:outline-none focus:ring-2 focus:ring-accent-indigo focus:border-transparent transition-all"
                    required
                  />
                </div>
                <button
                  type="button"
                  onClick={handleSendCode}
                  disabled={codeSending || countdown > 0 || !email}
                  className="px-4 py-3.5 rounded-xl bg-accent-indigo/10 text-accent-indigo font-medium text-sm whitespace-nowrap hover:bg-accent-indigo/20 transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
                >
                  {codeSending ? '发送中...' : countdown > 0 ? `${countdown}s` : '获取验证码'}
                </button>
              </div>
            </div>
          )}

          <button
            type="submit"
            disabled={isLoading}
            className="w-full py-3.5 rounded-xl bg-gradient-to-r from-accent-indigo to-[#4F46E5] text-white font-semibold text-base shadow-[0px_8px_24px_-4px_rgba(99,102,241,0.4)] hover:opacity-90 transition-opacity disabled:opacity-50"
          >
            {isLoading ? '登录中...' : '登录'}
          </button>
        </form>

        {/* 注册链接 */}
        <p className="text-center text-sm text-text-secondary">
          还没有账号？{' '}
          <Link href="/register" className="text-accent-indigo hover:underline font-medium">
            立即注册
          </Link>
        </p>

        {/* Demo 登录 */}
        <div className="pt-6 border-t border-border-subtle">
          <p className="text-sm text-text-tertiary text-center mb-4">快速体验（Demo 账号）</p>
          <div className="flex flex-col gap-3">
            <button
              type="button"
              onClick={() => handleDemoLogin('creator')}
              className="w-full p-4 text-left bg-bg-card border border-border-subtle rounded-xl hover:bg-bg-elevated transition-colors"
              disabled={isLoading}
            >
              <div className="font-medium text-text-primary">达人端</div>
              <div className="text-sm text-text-secondary">creator@demo.com</div>
            </button>
            <button
              type="button"
              onClick={() => handleDemoLogin('agency')}
              className="w-full p-4 text-left bg-bg-card border border-border-subtle rounded-xl hover:bg-bg-elevated transition-colors"
              disabled={isLoading}
            >
              <div className="font-medium text-text-primary">代理商端</div>
              <div className="text-sm text-text-secondary">agency@demo.com</div>
            </button>
            <button
              type="button"
              onClick={() => handleDemoLogin('brand')}
              className="w-full p-4 text-left bg-bg-card border border-border-subtle rounded-xl hover:bg-bg-elevated transition-colors"
              disabled={isLoading}
            >
              <div className="font-medium text-text-primary">品牌方端</div>
              <div className="text-sm text-text-secondary">brand@demo.com</div>
            </button>
          </div>
        </div>
      </div>
    </div>
  )
}

export default function LoginPage() {
  return (
    <Suspense fallback={
      <div className="min-h-screen bg-bg-page flex items-center justify-center">
        <div className="w-8 h-8 border-2 border-accent-indigo border-t-transparent rounded-full animate-spin" />
      </div>
    }>
      <LoginForm />
    </Suspense>
  )
}
