import 'outing_log_comment.dart';

class OutingLogModel {
  final String id;
  final String visitNo;
  final String salespersonId;
  final String salespersonName;
  final String deptId;
  final String deptName;
  final String customerId;
  final String customerName;
  final String contactId;
  final String contactName;
  final String contactPhone;
  final double? checkInLongitude;
  final double? checkInLatitude;
  final double gpsAccuracy;
  final String checkInAddress;
  final String visitSummary;
  final String nextPlan;
  final List<String> visitPhotos;
  final String status;
  final List<OutingLogComment> comments;
  final DateTime? lastViewedTime;
  final DateTime createTime;
  final DateTime updateTime;

  const OutingLogModel({
    required this.id,
    required this.visitNo,
    this.salespersonId = '',
    this.salespersonName = '',
    this.deptId = '',
    this.deptName = '',
    this.customerId = '',
    required this.customerName,
    this.contactId = '',
    this.contactName = '',
    this.contactPhone = '',
    this.checkInLongitude,
    this.checkInLatitude,
    this.gpsAccuracy = 15.0,
    this.checkInAddress = '',
    required this.visitSummary,
    this.nextPlan = '',
    this.visitPhotos = const [],
    this.status = 'completed',
    this.comments = const [],
    this.lastViewedTime,
    required this.createTime,
    required this.updateTime,
  });

  bool get hasNewComment {
    if (comments.isEmpty) return false;
    if (lastViewedTime == null) return true;
    return comments.any((c) => c.createTime.isAfter(lastViewedTime!));
  }

  bool get isDraft => status == 'draft';
  bool get isCompleted => status == 'completed';
  String get statusLabel => isDraft ? '草稿' : '已完成';
  String get statusCode => status;

  factory OutingLogModel.fromJson(Map<String, dynamic> json) {
    return OutingLogModel(
      id: json['id'] as String,
      visitNo: json['visitNo'] as String? ?? '',
      salespersonId: json['salespersonId'] as String? ?? '',
      salespersonName: json['salespersonName'] as String? ?? '',
      deptId: json['deptId'] as String? ?? '',
      deptName: json['deptName'] as String? ?? '',
      customerId: json['customerId'] as String? ?? '',
      customerName: json['customerName'] as String? ?? '',
      contactId: json['contactId'] as String? ?? '',
      contactName: json['contactName'] as String? ?? '',
      contactPhone: json['contactPhone'] as String? ?? '',
      checkInLongitude: (json['checkInLongitude'] as num?)?.toDouble(),
      checkInLatitude: (json['checkInLatitude'] as num?)?.toDouble(),
      gpsAccuracy: (json['gpsAccuracy'] as num?)?.toDouble() ?? 15.0,
      checkInAddress: json['checkInAddress'] as String? ?? '',
      visitSummary: json['visitSummary'] as String? ?? '',
      nextPlan: json['nextPlan'] as String? ?? '',
      visitPhotos:
          (json['visitPhotos'] as List<dynamic>?)
              ?.map((e) => e as String)
              .toList() ??
          [],
      status: json['status'] as String? ?? 'completed',
      comments:
          (json['comments'] as List<dynamic>?)
              ?.map((e) => OutingLogComment.fromJson(e as Map<String, dynamic>))
              .toList() ??
          [],
      lastViewedTime: json['lastViewedTime'] != null
          ? DateTime.parse(json['lastViewedTime'] as String)
          : null,
      createTime: DateTime.parse(json['createTime'] as String),
      updateTime: DateTime.parse(json['updateTime'] as String),
    );
  }

  Map<String, dynamic> toJson() => {
    'id': id,
    'visitNo': visitNo,
    'salespersonId': salespersonId,
    'salespersonName': salespersonName,
    'deptId': deptId,
    'deptName': deptName,
    'customerId': customerId,
    'customerName': customerName,
    'contactId': contactId,
    'contactName': contactName,
    'contactPhone': contactPhone,
    'checkInLongitude': checkInLongitude,
    'checkInLatitude': checkInLatitude,
    'gpsAccuracy': gpsAccuracy,
    'checkInAddress': checkInAddress,
    'visitSummary': visitSummary,
    'nextPlan': nextPlan,
    'visitPhotos': visitPhotos,
    'status': status,
    'comments': comments.map((c) => c.toJson()).toList(),
    'lastViewedTime': lastViewedTime?.toIso8601String(),
    'createTime': createTime.toIso8601String(),
    'updateTime': updateTime.toIso8601String(),
  };
}
