import React, { useState } from 'react';
import { Page } from '../types';

interface LoginProps {
    setPage: (page: Page) => void;
}

import { supabase } from '../supabaseClient';

const Login: React.FC<LoginProps> = ({ setPage }) => {
    const [email, setEmail] = useState('');
    const [password, setPassword] = useState('');
    const [loading, setLoading] = useState(false);

    const handleSubmit = async (e: React.FormEvent) => {
        e.preventDefault();
        setLoading(true);
        const { error } = await supabase.auth.signInWithPassword({
            email,
            password,
        });
        setLoading(false);

        if (error) {
            alert(error.message);
        } else {
            setPage(Page.DASHBOARD);
        }
    };

    return (
        <div className="min-h-screen bg-background-light dark:bg-background-dark pt-32 pb-12 px-6 flex items-center justify-center">
            <div className="w-full max-w-md bg-white dark:bg-slate-900 p-8 rounded-3xl shadow-xl border border-slate-100 dark:border-slate-800">
                <h2 className="font-display text-3xl mb-8 text-center text-slate-900 dark:text-white">Willkommen zurück</h2>
                <form onSubmit={handleSubmit} className="space-y-6">
                    <div>
                        <label className="block text-sm font-medium text-slate-700 dark:text-slate-300 mb-2">E-Mail Adresse</label>
                        <input
                            type="email"
                            required
                            className="w-full px-4 py-3 rounded-xl border border-slate-200 dark:border-slate-700 bg-slate-50 dark:bg-slate-800 focus:ring-2 focus:ring-primary outline-none transition-all"
                            value={email}
                            onChange={(e) => setEmail(e.target.value)}
                            placeholder="name@beispiel.de"
                        />
                    </div>
                    <div>
                        <label className="block text-sm font-medium text-slate-700 dark:text-slate-300 mb-2">Passwort</label>
                        <input
                            type="password"
                            required
                            className="w-full px-4 py-3 rounded-xl border border-slate-200 dark:border-slate-700 bg-slate-50 dark:bg-slate-800 focus:ring-2 focus:ring-primary outline-none transition-all"
                            value={password}
                            onChange={(e) => setPassword(e.target.value)}
                            placeholder="••••••••"
                        />
                    </div>
                    <button
                        type="submit"
                        disabled={loading}
                        className="w-full py-4 bg-primary text-white font-bold rounded-xl hover:bg-opacity-90 transition-all shadow-lg hover:shadow-primary/20 disabled:opacity-50 disabled:cursor-not-allowed"
                    >
                        {loading ? 'Laden...' : 'Einloggen'}
                    </button>
                </form>
                <p className="mt-8 text-center text-sm text-slate-500">
                    Noch kein Konto?{' '}
                    <button onClick={() => setPage(Page.REGISTER)} className="text-primary font-bold hover:underline">
                        Jetzt registrieren
                    </button>
                </p>
            </div>
        </div>
    );
};

export default Login;
