import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'core/config/env.dart';
import 'core/di/injection.dart';
import 'core/router/app_router.dart';
import 'core/theme/app_theme.dart';
import 'features/auth/data/models/auth_response.dart';
import 'features/auth/presentation/bloc/auth_bloc.dart';
import 'features/auth/presentation/bloc/auth_event.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await configureDependencies();

  final authBloc = sl<AuthBloc>();

  if (Env.isMockApi) {
    authBloc.add(
      AuthLoggedIn(
        user: AuthUser(id: 'user_001', email: 'test@example.com'),
      ),
    );
  } else {
    authBloc.add(AuthStarted());
  }

  runApp(LinksyApp(authBloc: authBloc));
}

class LinksyApp extends StatelessWidget {
  final AuthBloc authBloc;

  const LinksyApp({super.key, required this.authBloc});

  @override
  Widget build(BuildContext context) {
    return BlocProvider<AuthBloc>.value(
      value: authBloc,
      child: MaterialApp.router(
        title: 'Linksy',
        debugShowCheckedModeBanner: false,
        theme: AppTheme.light,
        routerConfig: createAppRouter(authBloc),
      ),
    );
  }
}
