import 'package:flutter/material.dart';
import 'package:Emma_home/screens/home.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
import 'utils/data.dart';
import 'package:provider/provider.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
//import 'package:learn_project/screens/account_page.dart';
//import 'package:learn_project/screens/login_page.dart';
//import 'package:learn_project/screens/splash_page.dart';

import 'package:flutter/services.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  try {
    await dotenv.load(fileName: ".env.local"); // Load environment variables
  } catch (e) {
    throw Exception('Error loading .env file: $e'); // Print error if any
  }
  await Supabase.initialize(
    url: dotenv.env['NEXT_PUBLIC_SUPABASE_URL'] ?? 'default_url',
    anonKey: dotenv.env['NEXT_PUBLIC_SUPABASE_ANON_KEY'] ?? 'default_key',
  );
  await autoLogin();
  var singleCategories = Categories.instance;
  singleCategories.updateCategoryAmount('Ingosltadt');
  runApp(
    ChangeNotifierProvider(
      create: (context) => Categories.instance,
      child: const MyApp(),
    ),
  );
}

/// Supabase client
final supabase = Supabase.instance.client;
const timeSpan = "";

/// Error message to display the user when unexpected error occurs.
const unexpectedErrorMessage = 'Unexpected error occurred.';
Future<void> autoLogin() async {
  final error = await supabase.auth.signInWithPassword(
    email: dotenv.env['NEXT_PRIVATE_SUPABASE_APPUSER'] ?? 'default_url',
    password: dotenv.env['NEXT_PRIVATE_SUPABASE_APPPWD'] ?? 'default_url',
  );
  print('Login fehlgeschlagen: $error');
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    //set initial orentation to langscape
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitDown,
      DeviceOrientation.portraitUp,
    ]);
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      //title: 'Recipes',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSwatch().copyWith(
          primary: const Color(0xff263341),
          secondary: const Color(0xff8DB646), // Your accent color
        ),
      ),
      home: const HomePage(),
      //home: MainScreen(),
    );
  }
}
