import Alpine from 'alpinejs';
import { fetchGames, fetchModes, requireAuth } from './game';

Alpine.data('home', () => ({
    loading: true,
    errorMessage: '',
    runningGames: [],
    abortedGames: [],
    finishedGames: [],
    modes: [],

    async init() {
        if (!await requireAuth()) return;

        try {
            const [gamesData, modesData] = await Promise.all([
                fetchGames(),
                fetchModes(),
            ]);

            this.runningGames = gamesData.running || [];
            this.abortedGames = gamesData.aborted || [];
            this.finishedGames = gamesData.finished || [];
            this.modes = modesData.modes || [];
        } catch (error) {
            this.errorMessage = error instanceof Error ? error.message : 'Daten konnten nicht geladen werden.';
        } finally {
            this.loading = false;
        }
    }
}));
