/**
 * Login form component.
 *
 * ⚠️ AUTO-GENERATED BY PRISM - DO NOT EDIT
 */

import React, { useState } from 'react';
import { useAuth } from '../../contexts/AuthContext';

export interface LoginFormProps {
  /** Called after successful login */
  onSuccess?: () => void;
  /** Called when user clicks signup link */
  onSignupClick?: () => void;
}

/**
 * Login form with email/username and password fields.
 */
export function LoginForm({ onSuccess, onSignupClick }: LoginFormProps): JSX.Element {
  const { login } = useAuth();
  const [formData, setFormData] = useState({
    {{ username_field }}: '',
    password: '',
  });
  const [error, setError] = useState<string | null>(null);
  const [loading, setLoading] = useState(false);

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

    try {
      await login(formData);
      onSuccess?.();
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Login failed');
    } finally {
      setLoading(false);
    }
  };

  const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    setFormData(prev => ({
      ...prev,
      [e.target.name]: e.target.value,
    }));
  };

  return (
    <div className="w-full max-w-md mx-auto">
      <div className="bg-white rounded-lg shadow-lg p-8">
        <h2 className="text-2xl font-bold text-nordic-800 mb-6 text-center">
          Sign In
        </h2>

        {error && (
          <div className="mb-4 p-3 bg-red-50 border border-red-200 rounded-md">
            <p className="text-sm text-red-800">{error}</p>
          </div>
        )}

        <form onSubmit={handleSubmit} className="space-y-4">
          <div>
            <label
              htmlFor="{{ username_field }}"
              className="block text-sm font-medium text-nordic-700 mb-1"
            >
              {{ username_field_title }}
            </label>
            <input
              type="{{ input_type }}"
              id="{{ username_field }}"
              name="{{ username_field }}"
              value={formData.{{ username_field }}}
              onChange={handleChange}
              required
              disabled={loading}
              className="w-full px-3 py-2 border border-nordic-200 rounded-md focus:outline-none focus:ring-2 focus:ring-nordic-500 disabled:bg-nordic-50 disabled:cursor-not-allowed"
              placeholder="Enter your {{ username_field }}"
            />
          </div>

          <div>
            <label
              htmlFor="password"
              className="block text-sm font-medium text-nordic-700 mb-1"
            >
              Password
            </label>
            <input
              type="password"
              id="password"
              name="password"
              value={formData.password}
              onChange={handleChange}
              required
              disabled={loading}
              className="w-full px-3 py-2 border border-nordic-200 rounded-md focus:outline-none focus:ring-2 focus:ring-nordic-500 disabled:bg-nordic-50 disabled:cursor-not-allowed"
              placeholder="Enter your password"
            />
          </div>

          <button
            type="submit"
            disabled={loading}
            className="w-full bg-nordic-600 text-white py-2 px-4 rounded-md hover:bg-nordic-700 focus:outline-none focus:ring-2 focus:ring-nordic-500 focus:ring-offset-2 disabled:bg-nordic-300 disabled:cursor-not-allowed transition-colors"
          >
            {loading ? 'Signing in...' : 'Sign In'}
          </button>
        </form>

        {onSignupClick && (
          <div className="mt-6 text-center">
            <p className="text-sm text-nordic-600">
              Don't have an account?{' '}
              <button
                type="button"
                onClick={onSignupClick}
                className="text-nordic-600 hover:text-nordic-800 font-medium underline"
              >
                Sign up
              </button>
            </p>
          </div>
        )}
      </div>
    </div>
  );
}
