// src/constants/theme.ts
import { MD3DarkTheme as DefaultTheme, configureFonts } from 'react-native-paper';
import { Platform } from 'react-native';
import { colors } from './colors';

// Define the font configuration using the names loaded in App.tsx
const fontConfig = {
  // Default configuration for all platforms
  regular: {
    fontFamily: 'Inter-Regular',
    fontWeight: 'normal' as 'normal', // Type assertion needed
  },
  medium: {
    fontFamily: 'Inter-Medium',
    fontWeight: '500' as '500', // Type assertion needed
  },
  light: {
    fontFamily: 'Inter-Light',
    fontWeight: '300' as '300', // Type assertion needed
  },
  thin: {
    fontFamily: 'Inter-Thin',
    fontWeight: '100' as '100', // Type assertion needed
  },
  // Add bold if needed and loaded
  bold: {
    fontFamily: 'Inter-Bold',
    fontWeight: 'bold' as 'bold', // Type assertion needed
  },

  // Define specific font variants used by Paper components
  // These map to the keys above
  displayLarge: { fontFamily: 'Inter-Regular', fontWeight: '400' as '400', fontSize: 57, lineHeight: 64, letterSpacing: -0.25 },
  displayMedium: { fontFamily: 'Inter-Regular', fontWeight: '400' as '400', fontSize: 45, lineHeight: 52, letterSpacing: 0 },
  displaySmall: { fontFamily: 'Inter-Regular', fontWeight: '400' as '400', fontSize: 36, lineHeight: 44, letterSpacing: 0 },

  headlineLarge: { fontFamily: 'Inter-Regular', fontWeight: '400' as '400', fontSize: 32, lineHeight: 40, letterSpacing: 0 },
  headlineMedium: { fontFamily: 'Inter-Regular', fontWeight: '400' as '400', fontSize: 28, lineHeight: 36, letterSpacing: 0 },
  headlineSmall: { fontFamily: 'Inter-Regular', fontWeight: '400' as '400', fontSize: 24, lineHeight: 32, letterSpacing: 0 },

  titleLarge: { fontFamily: 'Inter-Regular', fontWeight: '400' as '400', fontSize: 22, lineHeight: 28, letterSpacing: 0 },
  titleMedium: { fontFamily: 'Inter-Medium', fontWeight: '500' as '500', fontSize: 16, lineHeight: 24, letterSpacing: 0.15 },
  titleSmall: { fontFamily: 'Inter-Medium', fontWeight: '500' as '500', fontSize: 14, lineHeight: 20, letterSpacing: 0.1 },

  labelLarge: { fontFamily: 'Inter-Medium', fontWeight: '500' as '500', fontSize: 14, lineHeight: 20, letterSpacing: 0.1 },
  labelMedium: { fontFamily: 'Inter-Medium', fontWeight: '500' as '500', fontSize: 12, lineHeight: 16, letterSpacing: 0.5 },
  labelSmall: { fontFamily: 'Inter-Medium', fontWeight: '500' as '500', fontSize: 11, lineHeight: 16, letterSpacing: 0.5 },

  bodyLarge: { fontFamily: 'Inter-Regular', fontWeight: '400' as '400', fontSize: 16, lineHeight: 24, letterSpacing: 0.5 },
  bodyMedium: { fontFamily: 'Inter-Regular', fontWeight: '400' as '400', fontSize: 14, lineHeight: 20, letterSpacing: 0.25 },
  bodySmall: { fontFamily: 'Inter-Regular', fontWeight: '400' as '400', fontSize: 12, lineHeight: 16, letterSpacing: 0.4 },

  // Default is used as a fallback
  default: {
    fontFamily: 'Inter-Regular',
    fontWeight: 'normal' as 'normal',
  },
};


// Configure fonts for React Native Paper V5 (MD3)
// The structure differs slightly from V4
const fonts = configureFonts({
  config: fontConfig,
  // You might need specific web/ios/android configs if fonts differ
  // web: fontConfig,
  // ios: fontConfig,
  // android: fontConfig,
});


const theme = {
  ...DefaultTheme, // Use MD3 dark theme as a base
  fonts: fonts, // Apply the configured fonts
  colors: {
    ...DefaultTheme.colors, // Keep default colors unless overridden
    primary: colors.primary,
    accent: colors.secondary, // Note: Paper v5 uses 'secondary' often where 'accent' was used
    secondary: colors.secondary,
    tertiary: colors.secondary, // Assign accent to tertiary as well if needed
    background: colors.background,
    outline: colors.outline,
    outlineVariant: colors.outline,
    surface: colors.surface || colors.background, // Use surface or fallback to background
    text: colors.text,
    onPrimary: colors.background, // Text color on primary background
    onSecondary: colors.background, // Text color on secondary background
    onBackground: colors.text,      // Text color on main background
    onSurface: colors.text,        // Text color on surface background
    error: colors.error,
    disabled: colors.disabled,
    placeholder: colors.textSecondary,
    backdrop: 'rgba(0, 0, 0, 0.5)',
     // Adjust other colors as needed (surfaceVariant, primaryContainer, etc.)
    // You might need to experiment based on Paper v5's specific color roles
    primaryContainer: colors.primary, // Example adjustment
    secondaryContainer: colors.secondary, // Example adjustment

  },
   // You can customize other theme aspects like roundness, animation scale etc.
   roundness: 4,
};

export default theme;