'use client'

import { useState } from 'react'
import { signIn } from 'next-auth/react'
import { useRouter } from 'next/navigation'

export default function LoginPage() {
  const router = useRouter()
  const [email, setEmail] = useState('')
  const [password, setPassword] = useState('')
  const [error, setError] = useState('')
  const [loading, setLoading] = useState(false)

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

    const result = await signIn('credentials', {
      redirect: false,
      email,
      password,
    })

    if (result?.error) {
      setError('Geçersiz e-posta veya şifre')
      setLoading(false)
    } else {
      router.push('/admin')
      router.refresh()
    }
  }

  return (
    <div className="min-h-[100dvh] flex items-center justify-center bg-pine px-4 py-12">
      <div className="w-full max-w-md bg-paper rounded-3xl shadow-xl border border-white/10 overflow-hidden">
        <div className="p-8 sm:p-10 space-y-6">
          
          {/* Logo & Brand Header */}
          <div className="text-center space-y-3">
            <div className="inline-flex items-center justify-center w-12 h-12 rounded-full border-2 border-stone bg-paper shadow-sm">
              <span className="font-heading font-extrabold text-pine text-sm tracking-tighter">ML</span>
            </div>
            <h1 className="text-2xl font-heading font-extrabold text-pine tracking-tight lowercase">
              marmaris <span className="text-turquoise">local</span>
            </h1>
            <p className="text-xs text-stone-deep font-mono uppercase tracking-wider">
              backoffice admin login
            </p>
          </div>

          {error && (
            <div className="bg-bougainvillea/10 text-bougainvillea p-3.5 rounded-xl text-xs font-mono border border-bougainvillea/20 text-center font-semibold">
              {error}
            </div>
          )}

          <form onSubmit={handleSubmit} className="space-y-4">
            <div>
              <label className="block text-xs font-mono font-bold text-pine/80 uppercase tracking-wider mb-1.5" htmlFor="email">
                E-posta Adresi
              </label>
              <input
                id="email"
                type="email"
                value={email}
                onChange={(e) => setEmail(e.target.value)}
                required
                className="w-full px-4 py-3 border border-pine/15 rounded-xl focus:ring-2 focus:ring-turquoise focus:border-turquoise bg-stone/20 text-pine text-sm transition-colors outline-none font-medium min-h-[44px]"
                placeholder="admin@ayris.tech"
              />
            </div>
            <div>
              <label className="block text-xs font-mono font-bold text-pine/80 uppercase tracking-wider mb-1.5" htmlFor="password">
                Şifre
              </label>
              <input
                id="password"
                type="password"
                value={password}
                onChange={(e) => setPassword(e.target.value)}
                required
                className="w-full px-4 py-3 border border-pine/15 rounded-xl focus:ring-2 focus:ring-turquoise focus:border-turquoise bg-stone/20 text-pine text-sm transition-colors outline-none font-medium min-h-[44px]"
                placeholder="••••••••"
              />
            </div>
            <button
              type="submit"
              disabled={loading}
              className="w-full bg-turquoise hover:bg-turquoise/90 text-paper font-bold py-3.5 px-4 rounded-xl transition-all duration-150 active:scale-95 shadow-sm disabled:opacity-70 disabled:cursor-not-allowed cursor-pointer min-h-[44px] text-xs font-mono uppercase tracking-wider mt-2"
            >
              {loading ? 'Giriş yapılıyor...' : 'Giriş Yap'}
            </button>
          </form>
        </div>
      </div>
    </div>
  )
}
