enum WeaponType {
  handgun('Arme de Poing'),
  rifle('Arme d\'Épaule'),
  shotgun('Fusil à Pompe'),
  airgun('Airsoft / Airgun');

  final String displayName;
  const WeaponType(this.displayName);

  static WeaponType fromString(String value) {
    return WeaponType.values.firstWhere(
      (type) => type.name == value,
      orElse: () => WeaponType.handgun,
    );
  }
}

class Weapon {
  final String id;
  final String name;
  final WeaponType type;
  final String caliber;
  final int magazineCount;
  final int magazineCapacity;
  final String? notes;
  final DateTime createdAt;
  
  // New options
  final String? optic;
  final String? silencer;
  final String? trigger;
  final String? customName;

  String get displayName => (customName != null && customName!.isNotEmpty) ? customName! : name;

  Weapon({
    required this.id,
    required this.name,
    required this.type,
    required this.caliber,
    required this.magazineCount,
    required this.magazineCapacity,
    this.notes,
    required this.createdAt,
    this.optic,
    this.silencer,
    this.trigger,
    this.customName,
  });

  Weapon copyWith({
    String? id,
    String? name,
    WeaponType? type,
    String? caliber,
    int? magazineCount,
    int? magazineCapacity,
    String? notes,
    DateTime? createdAt,
    String? optic,
    String? silencer,
    String? trigger,
    String? customName,
  }) {
    return Weapon(
      id: id ?? this.id,
      name: name ?? this.name,
      type: type ?? this.type,
      caliber: caliber ?? this.caliber,
      magazineCount: magazineCount ?? this.magazineCount,
      magazineCapacity: magazineCapacity ?? this.magazineCapacity,
      notes: notes ?? this.notes,
      createdAt: createdAt ?? this.createdAt,
      optic: optic ?? this.optic,
      silencer: silencer ?? this.silencer,
      trigger: trigger ?? this.trigger,
      customName: customName ?? this.customName,
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'name': name,
      'type': type.name,
      'caliber': caliber,
      'magazine_count': magazineCount,
      'magazine_capacity': magazineCapacity,
      'notes': notes,
      'created_at': createdAt.toIso8601String(),
      'optic': optic,
      'silencer': silencer,
      'trigger': trigger,
      'custom_name': customName,
    };
  }

  factory Weapon.fromMap(Map<String, dynamic> map) {
    return Weapon(
      id: map['id'] as String,
      name: map['name'] as String,
      type: WeaponType.fromString(map['type'] as String),
      caliber: map['caliber'] as String,
      magazineCount: map['magazine_count'] as int,
      magazineCapacity: map['magazine_capacity'] as int,
      notes: map['notes'] as String?,
      createdAt: DateTime.parse(map['created_at'] as String),
      optic: map['optic'] as String?,
      silencer: map['silencer'] as String?,
      trigger: map['trigger'] as String?,
      customName: map['custom_name'] as String?,
    );
  }
}
