import 'package:flutter/material.dart';
import 'halo_tokens.dart';

ThemeData haloThemeData() {
  final base = ColorScheme.fromSeed(
    seedColor: HaloTokens.brand,
    brightness: Brightness.light,
  );

  final colorScheme = base.copyWith(
    primary: HaloTokens.brand,
    onPrimary: Colors.white,
    primaryContainer: HaloTokens.brandSoft,
    onPrimaryContainer: HaloTokens.brandDark,
    secondary: HaloTokens.accent,
    onSecondary: HaloTokens.ink,
    secondaryContainer: HaloTokens.accentSoft,
    onSecondaryContainer: HaloTokens.brandDark,
    surface: HaloTokens.surface,
    onSurface: HaloTokens.ink,
    surfaceContainerHighest: HaloTokens.bg,
    error: HaloTokens.danger,
    onError: Colors.white,
    outline: HaloTokens.border,
  );

  const textTheme = TextTheme(
    displayLarge: TextStyle(
      fontFamily: HaloTokens.fontDisplay,
      fontSize: 36,
      height: 40 / 36,
      fontWeight: FontWeight.w700,
      letterSpacing: -0.5,
      color: HaloTokens.ink,
    ),
    displayMedium: TextStyle(
      fontFamily: HaloTokens.fontDisplay,
      fontSize: 30,
      height: 34 / 30,
      fontWeight: FontWeight.w700,
      letterSpacing: -0.4,
      color: HaloTokens.ink,
    ),
    displaySmall: TextStyle(
      fontFamily: HaloTokens.fontDisplay,
      fontSize: 26,
      height: 30 / 26,
      fontWeight: FontWeight.w700,
      letterSpacing: -0.3,
      color: HaloTokens.ink,
    ),
    headlineMedium: TextStyle(
      fontFamily: HaloTokens.fontDisplay,
      fontSize: 22,
      height: 28 / 22,
      fontWeight: FontWeight.w700,
      color: HaloTokens.ink,
    ),
    titleLarge: TextStyle(
      fontFamily: HaloTokens.fontDisplay,
      fontSize: 22,
      height: 28 / 22,
      fontWeight: FontWeight.w700,
      color: HaloTokens.ink,
    ),
    titleMedium: TextStyle(
      fontFamily: HaloTokens.fontBody,
      fontSize: 18,
      height: 24 / 18,
      fontWeight: FontWeight.w600,
      color: HaloTokens.ink,
    ),
    titleSmall: TextStyle(
      fontFamily: HaloTokens.fontBody,
      fontSize: 15,
      height: 22 / 15,
      fontWeight: FontWeight.w600,
      color: HaloTokens.ink,
    ),
    bodyLarge: TextStyle(
      fontFamily: HaloTokens.fontBody,
      fontSize: 16,
      height: 24 / 16,
      fontWeight: FontWeight.w400,
      color: HaloTokens.ink,
    ),
    bodyMedium: TextStyle(
      fontFamily: HaloTokens.fontBody,
      fontSize: 15,
      height: 22 / 15,
      fontWeight: FontWeight.w400,
      color: HaloTokens.ink,
    ),
    bodySmall: TextStyle(
      fontFamily: HaloTokens.fontBody,
      fontSize: 13,
      height: 18 / 13,
      fontWeight: FontWeight.w500,
      color: HaloTokens.inkSoft,
    ),
    labelLarge: TextStyle(
      fontFamily: HaloTokens.fontBody,
      fontSize: 15,
      height: 22 / 15,
      fontWeight: FontWeight.w600,
      color: HaloTokens.ink,
    ),
    labelMedium: TextStyle(
      fontFamily: HaloTokens.fontBody,
      fontSize: 12,
      height: 16 / 12,
      fontWeight: FontWeight.w500,
      color: HaloTokens.inkSoft,
    ),
    labelSmall: TextStyle(
      fontFamily: HaloTokens.fontBody,
      fontSize: 10,
      height: 14 / 10,
      fontWeight: FontWeight.w600,
      letterSpacing: 0.4,
      color: HaloTokens.inkMuted,
    ),
  );

  return ThemeData(
    useMaterial3: true,
    colorScheme: colorScheme,
    scaffoldBackgroundColor: HaloTokens.bg,
    textTheme: textTheme,
    fontFamily: HaloTokens.fontBody,
    appBarTheme: const AppBarTheme(
      backgroundColor: HaloTokens.bg,
      foregroundColor: HaloTokens.ink,
      elevation: 0,
      scrolledUnderElevation: 0,
      centerTitle: false,
      titleTextStyle: TextStyle(
        fontFamily: HaloTokens.fontDisplay,
        fontSize: 22,
        height: 28 / 22,
        fontWeight: FontWeight.w700,
        color: HaloTokens.ink,
      ),
    ),
    elevatedButtonTheme: ElevatedButtonThemeData(
      style: ElevatedButton.styleFrom(
        backgroundColor: HaloTokens.brand,
        foregroundColor: Colors.white,
        disabledBackgroundColor: HaloTokens.brandSoft,
        disabledForegroundColor: HaloTokens.inkMuted,
        elevation: 0,
        shadowColor: const Color(0x59E17A9D),
        padding: const EdgeInsets.symmetric(
          horizontal: HaloSpacing.s24,
          vertical: HaloSpacing.s16,
        ),
        shape: const RoundedRectangleBorder(borderRadius: HaloRadius.pill),
        textStyle: const TextStyle(
          fontFamily: HaloTokens.fontBody,
          fontSize: 15,
          fontWeight: FontWeight.w600,
        ),
      ),
    ),
    outlinedButtonTheme: OutlinedButtonThemeData(
      style: OutlinedButton.styleFrom(
        foregroundColor: HaloTokens.brandDark,
        side: const BorderSide(color: HaloTokens.border),
        padding: const EdgeInsets.symmetric(
          horizontal: HaloSpacing.s24,
          vertical: HaloSpacing.s16,
        ),
        shape: const RoundedRectangleBorder(borderRadius: HaloRadius.pill),
        textStyle: const TextStyle(
          fontFamily: HaloTokens.fontBody,
          fontSize: 15,
          fontWeight: FontWeight.w600,
        ),
      ),
    ),
    textButtonTheme: TextButtonThemeData(
      style: TextButton.styleFrom(
        foregroundColor: HaloTokens.brandDark,
        padding: const EdgeInsets.symmetric(
          horizontal: HaloSpacing.s16,
          vertical: HaloSpacing.s12,
        ),
        shape: const RoundedRectangleBorder(borderRadius: HaloRadius.pill),
        textStyle: const TextStyle(
          fontFamily: HaloTokens.fontBody,
          fontSize: 15,
          fontWeight: FontWeight.w600,
        ),
      ),
    ),
    inputDecorationTheme: const InputDecorationTheme(
      filled: true,
      fillColor: HaloTokens.surface,
      contentPadding: EdgeInsets.symmetric(
        horizontal: HaloSpacing.s20,
        vertical: HaloSpacing.s20,
      ),
      constraints: BoxConstraints(minHeight: 64),
      hintStyle: TextStyle(
        fontFamily: HaloTokens.fontBody,
        fontSize: 15,
        color: HaloTokens.inkMuted,
      ),
      labelStyle: TextStyle(
        fontFamily: HaloTokens.fontBody,
        fontSize: 13,
        color: HaloTokens.inkSoft,
      ),
      border: OutlineInputBorder(
        borderRadius: HaloRadius.lg,
        borderSide: BorderSide(color: HaloTokens.border),
      ),
      enabledBorder: OutlineInputBorder(
        borderRadius: HaloRadius.lg,
        borderSide: BorderSide(color: HaloTokens.border),
      ),
      focusedBorder: OutlineInputBorder(
        borderRadius: HaloRadius.lg,
        borderSide: BorderSide(color: HaloTokens.brand, width: 2),
      ),
      errorBorder: OutlineInputBorder(
        borderRadius: HaloRadius.lg,
        borderSide: BorderSide(color: HaloTokens.danger),
      ),
      focusedErrorBorder: OutlineInputBorder(
        borderRadius: HaloRadius.lg,
        borderSide: BorderSide(color: HaloTokens.danger, width: 2),
      ),
    ),
    bottomSheetTheme: const BottomSheetThemeData(
      backgroundColor: HaloTokens.surface,
      surfaceTintColor: HaloTokens.surface,
      modalBackgroundColor: HaloTokens.surface,
      modalBarrierColor: Color(0x66000000),
      elevation: 0,
      modalElevation: 0,
      showDragHandle: true,
      dragHandleColor: HaloTokens.brandSoft,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(24),
          topRight: Radius.circular(24),
        ),
      ),
      clipBehavior: Clip.antiAlias,
    ),
    dialogTheme: const DialogThemeData(
      backgroundColor: HaloTokens.surface,
      surfaceTintColor: HaloTokens.surface,
      elevation: 0,
      shape: RoundedRectangleBorder(borderRadius: HaloRadius.xl),
      titleTextStyle: TextStyle(
        fontFamily: HaloTokens.fontDisplay,
        fontSize: 22,
        fontWeight: FontWeight.w700,
        color: HaloTokens.ink,
      ),
      contentTextStyle: TextStyle(
        fontFamily: HaloTokens.fontBody,
        fontSize: 15,
        height: 22 / 15,
        color: HaloTokens.inkSoft,
      ),
    ),
    snackBarTheme: const SnackBarThemeData(
      behavior: SnackBarBehavior.floating,
      backgroundColor: HaloTokens.ink,
      contentTextStyle: TextStyle(
        fontFamily: HaloTokens.fontBody,
        fontSize: 14,
        fontWeight: FontWeight.w500,
        color: Colors.white,
      ),
      shape: RoundedRectangleBorder(borderRadius: HaloRadius.pill),
      elevation: 4,
      insetPadding: EdgeInsets.symmetric(
        horizontal: HaloSpacing.s16,
        vertical: HaloSpacing.s12,
      ),
      actionTextColor: HaloTokens.brandSoft,
    ),
    chipTheme: ChipThemeData(
      backgroundColor: HaloTokens.surface,
      selectedColor: HaloTokens.brand,
      disabledColor: HaloTokens.brandSoft.withValues(alpha: 0.5),
      labelStyle: const TextStyle(
        fontFamily: HaloTokens.fontBody,
        fontSize: 14,
        fontWeight: FontWeight.w500,
        color: HaloTokens.ink,
      ),
      secondaryLabelStyle: const TextStyle(
        fontFamily: HaloTokens.fontBody,
        fontSize: 14,
        fontWeight: FontWeight.w500,
        color: Colors.white,
      ),
      padding: const EdgeInsets.symmetric(
        horizontal: HaloSpacing.s16,
        vertical: HaloSpacing.s8,
      ),
      side: const BorderSide(color: HaloTokens.border),
      shape: const RoundedRectangleBorder(borderRadius: HaloRadius.pill),
    ),
    dividerTheme: const DividerThemeData(
      color: HaloTokens.border,
      thickness: 1,
      space: 1,
    ),
  );
}
