'use client';

import { useActionState, useState } from 'react';
import { login } from './actions';
import packageJson from '@/package.json';

export default function LoginPage() {
  const [state, loginAction, isPending] = useActionState(login, undefined);
  const [showPassword, setShowPassword] = useState(false);

  const version = packageJson.version;
  const buildDate =
    process.env.NEXT_PUBLIC_BUILD_DATE ||
    new Date().toLocaleDateString('de-DE', { day: '2-digit', month: '2-digit', year: 'numeric' });

  return (
    <div className="min-h-screen bg-white py-4 px-4">
      <main className="max-w-6xl mx-auto border-2 border-black rounded-lg p-6 bg-[#EEF7EE]">
        <div className="flex justify-between items-center mb-6">
          <h1 className="text-3xl font-bold">Arbeitszeiterfassung</h1>
        </div>

        <div className="flex justify-center py-10">
          <div className="w-full max-w-sm bg-white border border-gray-300 rounded-xl shadow-md p-8">
            <h2 className="text-xl font-semibold text-gray-900 mb-6 text-center">Anmeldung</h2>

            <form action={loginAction} className="space-y-5">
              <div>
                <label htmlFor="username" className="block text-sm font-medium text-gray-700 mb-1">
                  Benutzer
                </label>
                <input
                  id="username"
                  name="username"
                  type="text"
                  required
                  defaultValue="Birgit"
                  autoComplete="username"
                  className="w-full px-3 py-2 border-2 border-gray-400 rounded-lg bg-white text-gray-900 focus:border-green-600 focus:outline-none text-sm"
                  placeholder="Benutzer"
                  disabled={isPending}
                />
              </div>

              <div>
                <label htmlFor="password" className="block text-sm font-medium text-gray-700 mb-1">
                  Passwort
                </label>
                <div className="relative">
                  <input
                    id="password"
                    name="password"
                    type={showPassword ? 'text' : 'password'}
                    required
                    autoComplete="current-password"
                    className="w-full px-3 py-2 pr-10 border-2 border-gray-400 rounded-lg bg-white text-gray-900 focus:border-green-600 focus:outline-none text-sm"
                    placeholder="Passwort"
                    disabled={isPending}
                  />
                  <button
                    type="button"
                    onClick={() => setShowPassword((v) => !v)}
                    tabIndex={-1}
                    aria-label={showPassword ? 'Passwort verbergen' : 'Passwort anzeigen'}
                    className="absolute inset-y-0 right-0 px-3 flex items-center text-gray-500 hover:text-gray-800"
                  >
                    {showPassword ? (
                      <svg xmlns="http://www.w3.org/2000/svg" className="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M13.875 18.825A10.05 10.05 0 0112 19c-4.478 0-8.268-2.943-9.543-7a9.97 9.97 0 011.563-3.029m5.858.908a3 3 0 114.243 4.243M9.878 9.878l4.242 4.242M9.88 9.88l-3.29-3.29m7.532 7.532l3.29 3.29M3 3l3.59 3.59m0 0A9.953 9.953 0 0112 5c4.478 0 8.268 2.943 9.543 7a10.025 10.025 0 01-4.132 4.411m0 0L21 21" />
                      </svg>
                    ) : (
                      <svg xmlns="http://www.w3.org/2000/svg" className="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
                        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z" />
                      </svg>
                    )}
                  </button>
                </div>
              </div>

              {state?.error && (
                <div className="bg-red-50 border border-red-300 text-red-700 px-3 py-2 rounded-lg text-sm">
                  {state.error}
                </div>
              )}

              <button
                type="submit"
                disabled={isPending}
                className="w-full py-2 px-4 bg-[#86C28B] hover:bg-[#6FB075] text-black font-medium rounded-lg transition-colors disabled:opacity-50 disabled:cursor-not-allowed text-sm"
              >
                {isPending ? 'Anmeldung läuft...' : 'Anmelden'}
              </button>
            </form>
          </div>
        </div>

        <footer className="mt-8 flex justify-between items-center text-sm text-gray-600 px-4">
          <div>
            <a href="mailto:rxf@gmx.de" className="text-green-700 hover:underline">
              rxf@gmx.de
            </a>
          </div>
          <div className="text-right">
            Version {version} — {buildDate}
          </div>
        </footer>
      </main>
    </div>
  );
}
