import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:go_router/go_router.dart';

import 'providers/auth_provider.dart';
import 'providers/dashboard_provider.dart';
import 'providers/article_provider.dart';
import 'providers/ventes_provider.dart';
import 'providers/achats_provider.dart';
import 'providers/production_provider.dart';
import 'providers/stock_provider.dart';
import 'screens/login_screen.dart';
import 'screens/dashboard_screen.dart';
import 'screens/articles_screen.dart';
import 'screens/ventes_screen.dart';
import 'screens/achats_screen.dart';
import 'screens/production_screen.dart';
import 'screens/stock_screen.dart';
import 'widgets/app_drawer.dart';

void main() {
  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => AuthProvider()),
        ChangeNotifierProvider(create: (_) => DashboardProvider()),
        ChangeNotifierProvider(create: (_) => ArticleProvider()),
        ChangeNotifierProvider(create: (_) => VentesProvider()),
        ChangeNotifierProvider(create: (_) => AchatsProvider()),
        ChangeNotifierProvider(create: (_) => ProductionProvider()),
        ChangeNotifierProvider(create: (_) => StockProvider()),
      ],
      child: const RayhanApp(),
    ),
  );
}

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

  @override
  Widget build(BuildContext context) {
    final authProvider = context.watch<AuthProvider>();

    final router = GoRouter(
      initialLocation: '/login',
      redirect: (context, state) {
        final loggedIn = authProvider.isAuthenticated;
        final onLogin = state.matchedLocation == '/login';
        if (!loggedIn && !onLogin) return '/login';
        if (loggedIn && onLogin) return '/dashboard';
        return null;
      },
      routes: [
        GoRoute(path: '/login', builder: (_, __) => const LoginScreen()),
        GoRoute(path: '/dashboard', builder: (_, __) => const DashboardScreen()),
        GoRoute(path: '/articles', builder: (_, __) => const ArticlesScreen()),
        GoRoute(path: '/ventes', builder: (_, __) => const VentesScreen()),
        GoRoute(path: '/achats', builder: (_, __) => const AchatsScreen()),
        GoRoute(path: '/production', builder: (_, __) => const ProductionScreen()),
        GoRoute(path: '/stock', builder: (_, __) => const StockScreen()),
      ],
    );

    return MaterialApp.router(
      title: 'Rayhan ERP',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(
          seedColor: const Color(0xFF1565C0),
          brightness: Brightness.light,
        ),
        useMaterial3: true,
        fontFamily: 'Roboto',
      ),
      routerConfig: router,
    );
  }
}

class _PlaceholderScreen extends StatelessWidget {
  final String title;
  final String route;
  const _PlaceholderScreen({required this.title, required this.route});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(title)),
      drawer: AppDrawer(currentRoute: route),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Icon(Icons.construction_outlined, size: 64, color: Colors.grey[400]),
            const SizedBox(height: 16),
            Text('Module $title',
                style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
            const SizedBox(height: 8),
            Text('En cours de développement',
                style: TextStyle(color: Colors.grey[600])),
          ],
        ),
      ),
    );
  }
}
