import 'dart:async';

import 'package:flutter/material.dart';

import 'package:listenmeister/main.dart';
import 'package:listenmeister/pages/lists.dart';
import 'package:listenmeister/pages/login.dart';

class AuthGate extends StatefulWidget {
  const AuthGate({super.key});

  @override
  State<AuthGate> createState() => _AuthGateState();
}

class _AuthGateState extends State<AuthGate> {
  StreamSubscription? _authSub;
  bool _loggedIn = false;

  @override
  void initState() {
    super.initState();
    _loggedIn = pb.authStore.isValid;
    _authSub = pb.authStore.onChange.listen((_) {
      setState(() => _loggedIn = pb.authStore.isValid);
    });
  }

  @override
  void dispose() {
    _authSub?.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (!_loggedIn) {
      return const LoginPage();
    } else {
      return const ListsPage();
    }
  }
}
