import { useState } from 'react'
import { useNavigate, useLocation } from 'react-router-dom'
import { Hotel } from 'lucide-react'

export function Login() {
  const [email, setEmail] = useState('')
  const [password, setPassword] = useState('')
  const [error, setError] = useState('')
  const [loading, setLoading] = useState(false)
  const navigate = useNavigate()
  const location = useLocation()
  const from = (location.state as { from?: string })?.from || '/'

  async function submit(e: React.FormEvent) {
    e.preventDefault()
    setLoading(true)
    setError('')
    try {
      const res = await fetch('/api/auth/login', {
        method: 'POST', credentials: 'include',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ email, password }),
      })
      if (res.ok) navigate(from, { replace: true })
      else setError('Invalid email or password')
    } catch {
      setError('Connection error — please try again')
    } finally {
      setLoading(false)
    }
  }

  return (
    <div style={{ height: '100dvh', display: 'flex', alignItems: 'center',
      justifyContent: 'center', padding: '1.5rem', background: 'var(--navy-dark)' }}>
      <div style={{ width: '100%', maxWidth: '360px' }}>
        <div style={{ textAlign: 'center', marginBottom: '2rem' }}>
          <div style={{ marginBottom: '0.5rem', color: 'var(--gold)' }}>
            <Hotel size={40} strokeWidth={1.75} />
          </div>
          <h1 style={{ fontSize: '1.5rem', color: 'var(--gold)', letterSpacing: '0.05em' }}>MANAGE</h1>
          <p style={{ color: 'var(--text-muted)', fontSize: '0.85rem', marginTop: '0.25rem' }}>
            {import.meta.env.VITE_HOTEL_NAME}
          </p>
        </div>

        <div style={{ background: 'var(--surface)', borderRadius: '12px',
          padding: '1.75rem', border: '1px solid var(--surface-2)' }}>
          <form onSubmit={submit} style={{ display: 'flex', flexDirection: 'column', gap: '0.875rem' }}>
            <input type="email" value={email} onChange={e => setEmail(e.target.value)}
              placeholder="Email address" required autoComplete="email" style={input} />
            <input type="password" value={password} onChange={e => setPassword(e.target.value)}
              placeholder="Password" required autoComplete="current-password" style={input} />
            {error && <p style={{ color: 'var(--danger)', fontSize: '0.85rem', textAlign: 'center' }}>{error}</p>}
            <button type="submit" disabled={loading} style={{
              background: loading ? 'var(--surface-2)' : 'var(--gold)',
              color: loading ? 'var(--text-muted)' : 'var(--navy-dark)',
              border: 'none', borderRadius: '8px', padding: '0.75rem',
              fontSize: '1rem', fontWeight: 700, marginTop: '0.25rem',
            }}>
              {loading ? 'Signing in…' : 'Sign in'}
            </button>
          </form>
        </div>
        <p style={{ textAlign: 'center', marginTop: '1rem', fontSize: '0.8rem', color: 'var(--text-muted)' }}>
          New employee?{' '}
          <a href="/register" style={{ color: 'var(--gold)', textDecoration: 'none' }}>
            Register with your Workforce email
          </a>
        </p>
      </div>
    </div>
  )
}

const input: React.CSSProperties = {
  background: 'var(--navy-dark)', border: '1px solid var(--surface-2)',
  borderRadius: '8px', color: 'var(--text)', padding: '0.7rem 0.875rem',
  fontSize: '1rem', width: '100%', outline: 'none',
}
