import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:glowwheels/models/shop_model.dart';
import 'package:glowwheels/provider/shop_provider.dart';
import 'package:provider/provider.dart';

import 'login_screen.dart';
import 'main_screen.dart';

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

  @override
  State<SplashDecider> createState() => _SplashDeciderState();
}

class _SplashDeciderState extends State<SplashDecider> {
  @override
  void initState() {
    super.initState();
    _checkLoginStatus();
  }

  Future<void> _checkLoginStatus() async {
    final shopBox = await Hive.openBox<ShopModel>('shopBox');
    final tokenBox = await Hive.openBox<String>('tokenBox');

    final shop = shopBox.get('shop');
    final token = tokenBox.get('token');

    if (shop != null && token != null && token.isNotEmpty) {
      // ✅ Set the provider values if needed
      Provider.of<ShopProvider>(context, listen: false).setShop(shop);
      Provider.of<ShopProvider>(context, listen: false).setToken(token);

      Navigator.pushReplacement(
        context,
        MaterialPageRoute(builder: (_) => MainScreen()),
      );
    } else {
      Navigator.pushReplacement(
        context,
        MaterialPageRoute(builder: (_) =>  LoginScreen()),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      body: Center(child: CircularProgressIndicator()),
    );
  }
}
