import 'package:flutter/material.dart';

import 'package:doormile/core/theme/app_colors.dart';

/// Central theme + reusable text styles based on the Doormile design system.
class AppTheme {
  AppTheme._();

  static const fontFamily = 'Public Sans';

  static ThemeData get light {
    final base = ThemeData.light(useMaterial3: true);

    return base.copyWith(
      scaffoldBackgroundColor: AppColors.surface,
      colorScheme: const ColorScheme.light(
        primary: AppColors.primary,
        onPrimary: AppColors.onPrimary,
        primaryContainer: AppColors.primaryContainer,
        onPrimaryContainer: AppColors.onPrimaryContainer,
        secondary: AppColors.secondary,
        onSecondary: Colors.white,
        tertiary: AppColors.tertiary,
        onTertiary: Colors.white,
        error: AppColors.error,
        surface: AppColors.surface,
        onSurface: AppColors.onSurface,
        surfaceContainerHighest: AppColors.surfaceContainerHighest,
        outline: AppColors.outline,
        outlineVariant: AppColors.outlineVariant,
      ),
      textTheme: base.textTheme.apply(
        fontFamily: fontFamily,
        bodyColor: AppColors.onSurface,
        displayColor: AppColors.onSurface,
      ),
      appBarTheme: const AppBarTheme(
        backgroundColor: Colors.transparent,
        elevation: 0,
        scrolledUnderElevation: 0,
        foregroundColor: AppColors.primary,
        centerTitle: false,
      ),
      inputDecorationTheme: InputDecorationTheme(
        filled: true,
        fillColor: AppColors.surfaceContainerLowest,
        contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 15),
        hintStyle: bodyMd.copyWith(color: AppColors.outline),
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(16),
          borderSide: const BorderSide(color: AppColors.outlineVariant),
        ),
        enabledBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(16),
          borderSide: const BorderSide(color: AppColors.outlineVariant),
        ),
        focusedBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(16),
          borderSide: const BorderSide(color: AppColors.primary, width: 1.4),
        ),
      ),
      splashFactory: InkRipple.splashFactory,
    );
  }

  static const _f = fontFamily;

  /// Builds a style backed by the variable font's true `wght` axis so weights
  /// render at their real glyph widths (no synthetic-bold widening).
  static TextStyle _ts(double size, int wght, double lineHeight, [Color? color, double? letterSpacing]) {
    return TextStyle(
      fontFamily: _f,
      fontSize: size,
      height: lineHeight / size,
      letterSpacing: letterSpacing,
      color: color ?? AppColors.onSurface,
      fontWeight: FontWeight.values[(wght ~/ 100) - 1],
      fontVariations: [FontVariation('wght', wght.toDouble())],
    );
  }

  // ---- Typography helpers (mirror DESIGN.md scale) ----
  static final TextStyle headlineLg = _ts(22, 800, 28, null, -0.5);
  static final TextStyle headlineMd = _ts(17, 700, 24, null, -0.3);
  static final TextStyle headlineSm = _ts(15, 700, 22, null, -0.2);
  static final TextStyle bodyLg = _ts(15, 400, 22);
  static final TextStyle bodyMd = _ts(13, 400, 20);
  static final TextStyle labelMd = _ts(13, 700, 20);
  static final TextStyle caption = _ts(11, 500, 16, AppColors.onSurfaceVariant);
  static final TextStyle button = _ts(14, 700, 20, null, 0.2);

  /// Ambient card shadow (Level 1 elevation from the design brief).
  static List<BoxShadow> get cardShadow => const [
        BoxShadow(color: Color(0x12000000), blurRadius: 28, offset: Offset(0, 12)),
        BoxShadow(color: Color(0x08C31723), blurRadius: 8, offset: Offset(0, 2)),
      ];
}
