import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter/material.dart';
import 'package:hive_flutter/hive_flutter.dart';
import '../models/user_profile.dart';
import '../theme/app_theme.dart';

// Provider for the UserProfile box
final userProfileBoxProvider = Provider<Box<UserProfile>>((ref) {
  throw UnimplementedError('Provider was not initialized');
});

// Central Notifier for User Profile (Font, Theme, Identity)
final userProfileProvider = NotifierProvider<UserProfileNotifier, UserProfile>(UserProfileNotifier.new);

class UserProfileNotifier extends Notifier<UserProfile> {
  late Box<UserProfile> _box;

  @override
  UserProfile build() {
    _box = ref.watch(userProfileBoxProvider);
    // Return existing profile or create default
    return _box.get('current_user') ?? 
           UserProfile(createdAt: DateTime.now());
  }

  Future<void> _save(UserProfile profile) async {
    state = profile; // Update UI immediately
    await _box.put('current_user', profile);
  }

  Future<void> setFontPreference(String preference) async {
    final newState = state.copyWith(
      fontPreference: preference,
      updatedAt: DateTime.now(),
    );
    await _save(newState);
  }

  Future<void> setThemeMode(String mode) async {
    final newState = state.copyWith(
      themeMode: mode,
      updatedAt: DateTime.now(),
    );
    await _save(newState);
  }

  Future<void> setIdentity({String? mbti, DateTime? birthdate, String? nickname, String? gender}) async {
    final newState = state.copyWith(
      mbti: mbti ?? state.mbti,
      birthdate: birthdate ?? state.birthdate,
      nickname: nickname ?? state.nickname,
      gender: gender ?? state.gender,
      updatedAt: DateTime.now(),
    );
    await _save(newState);
  }

  Future<void> toggleBusinessMode() async {
     final newState = state.copyWith(
       isBusinessMode: !state.isBusinessMode,
       updatedAt: DateTime.now(),
     );
     await _save(newState);
  }

  Future<void> setDefaultMarkup(double value) async {
     final newState = state.copyWith(
       defaultMarkup: value,
       updatedAt: DateTime.now(),
     );
     await _save(newState);
  }

  Future<void> completeOnboarding() async {
     final newState = state.copyWith(
       hasCompletedOnboarding: true,
       updatedAt: DateTime.now(),
     );
     await _save(newState);
  }

  Future<void> updateTotalExp(int newExp) async {
    final newState = state.copyWith(
      totalExp: newExp,
      updatedAt: DateTime.now(),
    );
    await _save(newState);
  }
}

// Helper Providers for easy access
final fontPreferenceProvider = Provider<String>((ref) {
  return ref.watch(userProfileProvider).fontPreference;
});

final themeModeProvider = Provider<ThemeMode>((ref) {
  final mode = ref.watch(userProfileProvider).themeMode;
  switch (mode) {
    case 'light': return ThemeMode.light;
    case 'dark': return ThemeMode.dark;
    default: return ThemeMode.system;
  }
});

// Theme Data Providers
final lightThemeProvider = Provider<ThemeData>((ref) {
  final font = ref.watch(fontPreferenceProvider);
  return AppTheme.createTheme(font, Brightness.light);
});

final darkThemeProvider = Provider<ThemeData>((ref) {
  final font = ref.watch(fontPreferenceProvider);
  return AppTheme.createTheme(font, Brightness.dark);
});
