import 'shot.dart';
import 'target_type.dart';

class TargetAnalysis {
  final String id;
  final String sessionId;
  final TargetType targetType;
  final String imagePath;
  final List<Shot> shots;
  final int totalScore;
  final double? groupingDiameter;
  final double? groupingCenterX;
  final double? groupingCenterY;
  final DateTime createdAt;
  final String? notes;

  // Target detection data
  final double? targetCenterX;
  final double? targetCenterY;
  final double? targetRadius;

  TargetAnalysis({
    required this.id,
    required this.sessionId,
    required this.targetType,
    required this.imagePath,
    required this.shots,
    required this.totalScore,
    this.groupingDiameter,
    this.groupingCenterX,
    this.groupingCenterY,
    required this.createdAt,
    this.notes,
    this.targetCenterX,
    this.targetCenterY,
    this.targetRadius,
  });

  int get shotCount => shots.length;

  double get averageScore => shots.isEmpty ? 0.0 : totalScore / shots.length;

  TargetAnalysis copyWith({
    String? id,
    String? sessionId,
    TargetType? targetType,
    String? imagePath,
    List<Shot>? shots,
    int? totalScore,
    double? groupingDiameter,
    double? groupingCenterX,
    double? groupingCenterY,
    DateTime? createdAt,
    String? notes,
    double? targetCenterX,
    double? targetCenterY,
    double? targetRadius,
  }) {
    return TargetAnalysis(
      id: id ?? this.id,
      sessionId: sessionId ?? this.sessionId,
      targetType: targetType ?? this.targetType,
      imagePath: imagePath ?? this.imagePath,
      shots: shots ?? this.shots,
      totalScore: totalScore ?? this.totalScore,
      groupingDiameter: groupingDiameter ?? this.groupingDiameter,
      groupingCenterX: groupingCenterX ?? this.groupingCenterX,
      groupingCenterY: groupingCenterY ?? this.groupingCenterY,
      createdAt: createdAt ?? this.createdAt,
      notes: notes ?? this.notes,
      targetCenterX: targetCenterX ?? this.targetCenterX,
      targetCenterY: targetCenterY ?? this.targetCenterY,
      targetRadius: targetRadius ?? this.targetRadius,
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'session_id': sessionId,
      'target_type': targetType.name,
      'image_path': imagePath,
      'total_score': totalScore,
      'grouping_diameter': groupingDiameter,
      'grouping_center_x': groupingCenterX,
      'grouping_center_y': groupingCenterY,
      'created_at': createdAt.toIso8601String(),
      'notes': notes,
      'target_center_x': targetCenterX,
      'target_center_y': targetCenterY,
      'target_radius': targetRadius,
    };
  }

  factory TargetAnalysis.fromMap(Map<String, dynamic> map, List<Shot> shots) {
    return TargetAnalysis(
      id: map['id'] as String,
      sessionId: map['session_id'] as String,
      targetType: TargetType.fromString(map['target_type'] as String),
      imagePath: map['image_path'] as String,
      shots: shots,
      totalScore: map['total_score'] as int,
      groupingDiameter: map['grouping_diameter'] as double?,
      groupingCenterX: map['grouping_center_x'] as double?,
      groupingCenterY: map['grouping_center_y'] as double?,
      createdAt: DateTime.parse(map['created_at'] as String),
      notes: map['notes'] as String?,
      targetCenterX: map['target_center_x'] as double?,
      targetCenterY: map['target_center_y'] as double?,
      targetRadius: map['target_radius'] as double?,
    );
  }
}
