import { useState } from 'react';
import { useNavigate, Link } from 'react-router-dom';
import { useAuth } from '../context/AuthContext';
import Logo from '../components/Logo';

export default function RegisterPage() {
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');
  const [error, setError] = useState('');
  const [loading, setLoading] = useState(false);
  const { register } = useAuth();
  const navigate = useNavigate();

  const handleSubmit = async (e) => {
    e.preventDefault();
    setError('');
    setLoading(true);
    try {
      await register(email, password);
      navigate('/onboarding');
    } catch (err) {
      setError(err.message);
    }
    setLoading(false);
  };

  return (
    <div className="auth-page">
      <div className="auth-card">
        <Logo />
        <h1 className="auth-title">Skapa konto</h1>
        <p className="auth-tagline">Börja din träningsresa</p>
        {error && <div className="error auth-error">{error}</div>}
        <form onSubmit={handleSubmit}>
          <input type="email" placeholder="E-post" value={email} onChange={e => setEmail(e.target.value)} required />
          <input type="password" placeholder="Lösenord" value={password} onChange={e => setPassword(e.target.value)} required minLength={6} />
          <button type="submit" disabled={loading}>{loading ? 'Skapar...' : 'Skapa konto'}</button>
        </form>
        <p className="auth-link">Har redan konto? <Link to="/login">Logga in</Link></p>
      </div>
    </div>
  );
}
