import 'package:hive/hive.dart';
import '../services/level_calculator.dart';

part 'user_profile.g.dart';

@HiveType(typeId: 1)
class UserProfile extends HiveObject {
  @HiveField(0)
  // DEPRECATED: String? mbtiType; 
  // Hive indices must not be reused or reordered carelessly.
  // Ideally we keep the index but ignore it, or just use the old index for new logic.
  // The user review suggested: "Remove old fields... or unify".
  // Since we already used new indices (6, 7) in the previous step and ran build_runner, 
  // keeping 6 and 7 is safer if we want to avoid migration issues with recently created data.
  // But since this is dev, clean up is better.
  // However, I will follow the user's advice to remove the old ones.
  // I will comment them out to reserve the index if needed, or just remove.
  // Let's remove them and their constructor args.
  
  @HiveField(2)
  String fontPreference; // "serif" or "sans"

  @HiveField(3)
  DateTime createdAt;

  @HiveField(4)
  DateTime? updatedAt;

  @HiveField(5)
  String displayMode; // "list" or "grid"

  @HiveField(6)
  String? mbti; // 本物のMBTI（ユーザー入力）

  @HiveField(21) // Appended new field
  String? sakePersonaMbti; // AI診断による酒向タイプ

  @HiveField(7)
  DateTime? birthdate;

  @HiveField(8)
  String themeMode; // 'system', 'light', 'dark'

  @HiveField(9, defaultValue: false)
  bool isBusinessMode;

  @HiveField(10, defaultValue: 3.0)
  double defaultMarkup;

  @HiveField(11, defaultValue: false)
  bool hasCompletedOnboarding;

  @HiveField(12)
  String? nickname;

  @HiveField(13)
  String? gender; // 'male', 'female', 'other', null

  @HiveField(14, defaultValue: 0)
  int totalExp;

  @HiveField(15, defaultValue: [])
  List<String> unlockedBadges;

  // DEPRECATED: Tutorial flags no longer used (simplified to guide screen only)
  // HiveField 16, 17, 18 removed.

  @HiveField(19, defaultValue: 'ja')
  String locale; // 'ja', 'en', 'fr', 'de'

  @HiveField(20, defaultValue: 'washi_sumi_kohaku')
  String colorVariant; // 'washi_sumi_kohaku' (Theme A), 'current' (Theme B)

  // Calculators
  int get level => LevelCalculator.getLevel(totalExp);
  String get title => LevelCalculator.getTitle(totalExp);
  double get nextLevelProgress => LevelCalculator.getProgress(totalExp);
  int get expToNextLevel => LevelCalculator.getExpToNextLevel(totalExp);

  UserProfile({
    this.fontPreference = 'sans',
    this.displayMode = 'list',
    required this.createdAt,
    this.updatedAt,
    this.mbti,
    this.sakePersonaMbti,
    this.birthdate,
    this.themeMode = 'system',
    this.isBusinessMode = false,
    this.defaultMarkup = 3.0,
    this.hasCompletedOnboarding = false,
    this.nickname,
    this.gender,
    this.totalExp = 0,
    this.unlockedBadges = const [],
    this.locale = 'ja',
    this.colorVariant = 'washi_sumi_kohaku',
  });

  UserProfile copyWith({
    String? fontPreference,
    String? displayMode,
    DateTime? createdAt,
    DateTime? updatedAt,
    String? mbti,
    String? sakePersonaMbti,
    DateTime? birthdate,
    String? themeMode,
    bool? isBusinessMode,
    double? defaultMarkup,
    bool? hasCompletedOnboarding,
    String? nickname,
    String? gender,
    int? totalExp,
    List<String>? unlockedBadges,
    // REMOVED: Tutorial parameters (system removed in favor of guide screen)
    String? locale,
    String? colorVariant,
  }) {
    return UserProfile(
      fontPreference: fontPreference ?? this.fontPreference,
      displayMode: displayMode ?? this.displayMode,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      mbti: mbti ?? this.mbti,
      sakePersonaMbti: sakePersonaMbti ?? this.sakePersonaMbti,
      birthdate: birthdate ?? this.birthdate,
      themeMode: themeMode ?? this.themeMode,
      isBusinessMode: isBusinessMode ?? this.isBusinessMode,
      defaultMarkup: defaultMarkup ?? this.defaultMarkup,
      hasCompletedOnboarding: hasCompletedOnboarding ?? this.hasCompletedOnboarding,
      nickname: nickname ?? this.nickname,
      gender: gender ?? this.gender,
      totalExp: totalExp ?? this.totalExp,
      unlockedBadges: unlockedBadges ?? this.unlockedBadges,
      // Tutorial fields removed
      locale: locale ?? this.locale,
      colorVariant: colorVariant ?? this.colorVariant,
    );
  }
}
