import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:shadcn_flutter/shadcn_flutter.dart';
import 'core/api/pocketbase_client.dart';
import 'core/providers/locale_provider.dart';
import 'core/router/router_provider.dart';
import 'core/services/notification_service.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await PocketBaseClient.init();
  await NotificationService.init();

  final initialLocale = await LocaleNotifier.load();

  runApp(
    ProviderScope(
      overrides: [
        localeProvider.overrideWith(
          (ref) => LocaleNotifier(initialLocale),
        ),
      ],
      child: const DlsApp(),
    ),
  );
}

class DlsApp extends ConsumerWidget {
  const DlsApp({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final router = ref.watch(routerProvider);
    final locale = ref.watch(localeProvider);
    NotificationService.setRouter(router);
    return ShadcnApp.router(
      title: 'DLS',
      debugShowCheckedModeBanner: false,
      locale: locale,
      supportedLocales: supportedLocales,
      localizationsDelegates: const [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      theme: const ThemeData(
        colorScheme: _dlsLight,
        radius: 0.5,
      ),
      darkTheme: const ThemeData.dark(
        colorScheme: _dlsDark,
        radius: 0.5,
      ),
      routerConfig: router,
    );
  }
}

// ── DLS Light color scheme ────────────────────────────────────────────────────

const _dlsLight = ColorScheme(
  brightness: Brightness.light,
  background: Color(0xFFF1F5F9),
  foreground: Color(0xFF0F172A),
  card: Color(0xFFFFFFFF),
  cardForeground: Color(0xFF0F172A),
  popover: Color(0xFFFFFFFF),
  popoverForeground: Color(0xFF0F172A),
  primary: Color(0xFF1E3A5F),
  primaryForeground: Color(0xFFFFFFFF),
  secondary: Color(0xFFE2E8F0),
  secondaryForeground: Color(0xFF0F172A),
  muted: Color(0xFFE2E8F0),
  mutedForeground: Color(0xFF64748B),
  accent: Color(0xFFF8FAFC),
  accentForeground: Color(0xFF0F172A),
  destructive: Color(0xFFDC2626),
  border: Color(0xFFE2E8F0),
  input: Color(0xFFE2E8F0),
  ring: Color(0xFF0369A1),
  chart1: Color(0xFF1E3A5F),
  chart2: Color(0xFF059669),
  chart3: Color(0xFFF59E0B),
  chart4: Color(0xFF0369A1),
  chart5: Color(0xFFDC2626),
  sidebar: Color(0xFFFFFFFF),
  sidebarForeground: Color(0xFF0F172A),
  sidebarPrimary: Color(0xFF1E3A5F),
  sidebarPrimaryForeground: Color(0xFFFFFFFF),
  sidebarAccent: Color(0xFFF1F5F9),
  sidebarAccentForeground: Color(0xFF0F172A),
  sidebarBorder: Color(0xFFE2E8F0),
  sidebarRing: Color(0xFF0369A1),
);

// ── DLS Dark color scheme ─────────────────────────────────────────────────────

const _dlsDark = ColorScheme(
  brightness: Brightness.dark,
  background: Color(0xFF0F172A),
  foreground: Color(0xFFF1F5F9),
  card: Color(0xFF1E293B),
  cardForeground: Color(0xFFF1F5F9),
  popover: Color(0xFF1E293B),
  popoverForeground: Color(0xFFF1F5F9),
  primary: Color(0xFF93C5FD),
  primaryForeground: Color(0xFF1E3A5F),
  secondary: Color(0xFF273344),
  secondaryForeground: Color(0xFFF1F5F9),
  muted: Color(0xFF273344),
  mutedForeground: Color(0xFF94A3B8),
  accent: Color(0xFF273344),
  accentForeground: Color(0xFFF1F5F9),
  destructive: Color(0xFFFCA5A5),
  border: Color(0xFF334155),
  input: Color(0xFF334155),
  ring: Color(0xFF7DD3FC),
  chart1: Color(0xFF93C5FD),
  chart2: Color(0xFF6EE7B7),
  chart3: Color(0xFFFCD34D),
  chart4: Color(0xFF7DD3FC),
  chart5: Color(0xFFFCA5A5),
  sidebar: Color(0xFF1E293B),
  sidebarForeground: Color(0xFFF1F5F9),
  sidebarPrimary: Color(0xFF93C5FD),
  sidebarPrimaryForeground: Color(0xFF1E3A5F),
  sidebarAccent: Color(0xFF273344),
  sidebarAccentForeground: Color(0xFFF1F5F9),
  sidebarBorder: Color(0xFF334155),
  sidebarRing: Color(0xFF7DD3FC),
);
