'use client';

import { useState, type FormEvent } from 'react';
import Image from 'next/image';
import { signIn } from 'next-auth/react';
import { useRouter, useParams } from 'next/navigation';
import { BrandName } from '@/components/ui/brand-name';
import { siteConfig } from '@/data/site-config';

export default function AdminLoginPage() {
  const router = useRouter();
  const params = useParams();
  const locale = params.locale as string;
  const [error, setError] = useState<string | null>(null);
  const [loading, setLoading] = useState(false);

  async function handleSubmit(e: FormEvent<HTMLFormElement>) {
    e.preventDefault();
    setLoading(true);
    setError(null);
    const form = new FormData(e.currentTarget);

    const res = await signIn('credentials', {
      email: form.get('email'),
      password: form.get('password'),
      redirect: false
    });

    setLoading(false);
    if (res?.error) {
      setError('Identifiants invalides.');
      return;
    }
    router.push(`/${locale}/admin`);
    router.refresh();
  }

  return (
    <div className="flex min-h-screen items-center justify-center bg-graphite-950 px-4">
      <form onSubmit={handleSubmit} className="w-full max-w-sm rounded-2xl bg-white p-8">
        <div className="flex flex-col items-center gap-3 text-center">
          <Image
            src="/images/logo-square.jpg"
            alt={`${siteConfig.companyName} logo`}
            width={64}
            height={64}
            className="h-16 w-16 rounded-2xl object-cover ring-2 ring-copper-500/20"
          />
          <div>
            <h1 className="font-display text-xl font-semibold">
              <BrandName className="text-graphite-900" accentClass="text-copper-600" />
            </h1>
            <p className="text-sm text-graphite-900/50">Connexion admin</p>
          </div>
        </div>
        <div className="mt-6 space-y-4">
          <div>
            <label className="mb-1 block text-sm font-medium">E-mail</label>
            <input
              name="email"
              type="email"
              defaultValue="admin@example.com"
              required
              className="w-full rounded-lg border border-graphite-800/20 px-3 py-2 text-sm"
            />
          </div>
          <div>
            <label className="mb-1 block text-sm font-medium">Mot de passe</label>
            <input
              name="password"
              type="password"
              defaultValue="admin123"
              required
              className="w-full rounded-lg border border-graphite-800/20 px-3 py-2 text-sm"
            />
          </div>
        </div>
        {error && <p className="mt-4 text-sm text-copper-600">{error}</p>}
        <button
          type="submit"
          disabled={loading}
          className="mt-6 w-full rounded-full bg-copper-500 py-2.5 text-sm font-semibold text-graphite-950 hover:bg-copper-400 disabled:opacity-50"
        >
          {loading ? '…' : 'Se connecter'}
        </button>
      </form>
    </div>
  );
}
