import '../../shared/models/approval_status.dart';

class VehicleModel {
  final String id;
  final String applicationNo;
  final String applicantId;
  final String applicantName;
  final String deptId;
  final String deptName;
  final String vehicleType;
  final String purpose;
  final DateTime startTime;
  final DateTime endTime;
  final String origin;
  final String destination;
  final int passengerCount;
  final String driver;
  final String licensePlate;
  final double estimatedMileage;
  final double estimatedCost;
  final String reason;
  final String status;
  final String currentApproverId;
  final List<String> approvalChain;
  final DateTime createTime;
  final DateTime updateTime;
  final double actualMileage;
  final double actualCost;
  final double startOdometer;
  final double endOdometer;
  final DateTime? returnTime;
  final List<String> passengers;
  final List<ApprovalRecord> approvalRecords;

  const VehicleModel({
    required this.id,
    required this.applicationNo,
    required this.applicantId,
    required this.applicantName,
    required this.deptId,
    required this.deptName,
    this.vehicleType = '轿车',
    required this.purpose,
    required this.startTime,
    required this.endTime,
    required this.origin,
    required this.destination,
    this.passengerCount = 1,
    this.driver = '',
    this.licensePlate = '',
    this.estimatedMileage = 0.0,
    this.estimatedCost = 0.0,
    required this.reason,
    this.status = 'draft',
    this.currentApproverId = '',
    this.approvalChain = const [],
    required this.createTime,
    required this.updateTime,
    this.actualMileage = 0.0,
    this.actualCost = 0.0,
    this.startOdometer = 0.0,
    this.endOdometer = 0.0,
    this.returnTime,
    this.passengers = const [],
    this.approvalRecords = const [],
  });

  factory VehicleModel.fromJson(Map<String, dynamic> json) {
    return VehicleModel(
      id: json['id'] as String,
      applicationNo: json['applicationNo'] as String? ?? '',
      applicantId: json['applicantId'] as String? ?? '',
      applicantName: json['applicantName'] as String? ?? '',
      deptId: json['deptId'] as String? ?? '',
      deptName: json['deptName'] as String? ?? '',
      vehicleType: json['vehicleType'] as String? ?? '轿车',
      purpose: json['purpose'] as String? ?? '',
      startTime: DateTime.parse(json['startTime'] as String),
      endTime: DateTime.parse(json['endTime'] as String),
      origin: json['origin'] as String? ?? '',
      destination: json['destination'] as String? ?? '',
      passengerCount: json['passengerCount'] as int? ?? 1,
      driver: json['driver'] as String? ?? '',
      licensePlate: json['licensePlate'] as String? ?? '',
      estimatedMileage: (json['estimatedMileage'] as num?)?.toDouble() ?? 0.0,
      estimatedCost: (json['estimatedCost'] as num?)?.toDouble() ?? 0.0,
      reason: json['reason'] as String? ?? '',
      status: json['status'] as String? ?? 'draft',
      currentApproverId: json['currentApproverId'] as String? ?? '',
      approvalChain:
          (json['approvalChain'] as List<dynamic>?)
              ?.map((e) => e as String)
              .toList() ??
          [],
      createTime: DateTime.parse(json['createTime'] as String),
      updateTime: DateTime.parse(json['updateTime'] as String),
      actualMileage: (json['actualMileage'] as num?)?.toDouble() ?? 0.0,
      actualCost: (json['actualCost'] as num?)?.toDouble() ?? 0.0,
      startOdometer: (json['startOdometer'] as num?)?.toDouble() ?? 0.0,
      endOdometer: (json['endOdometer'] as num?)?.toDouble() ?? 0.0,
      returnTime: json['returnTime'] != null
          ? DateTime.parse(json['returnTime'] as String)
          : null,
      passengers:
          (json['passengers'] as List<dynamic>?)
              ?.map((e) => e as String)
              .toList() ??
          [],
      approvalRecords:
          (json['approvalRecords'] as List<dynamic>?)
              ?.map((e) => ApprovalRecord.fromJson(e as Map<String, dynamic>))
              .toList() ??
          [],
    );
  }

  Map<String, dynamic> toJson() => {
    'id': id,
    'applicationNo': applicationNo,
    'applicantId': applicantId,
    'applicantName': applicantName,
    'deptId': deptId,
    'deptName': deptName,
    'vehicleType': vehicleType,
    'purpose': purpose,
    'startTime': startTime.toIso8601String(),
    'endTime': endTime.toIso8601String(),
    'origin': origin,
    'destination': destination,
    'passengerCount': passengerCount,
    'driver': driver,
    'licensePlate': licensePlate,
    'estimatedMileage': estimatedMileage,
    'estimatedCost': estimatedCost,
    'reason': reason,
    'status': status,
    'currentApproverId': currentApproverId,
    'approvalChain': approvalChain,
    'createTime': createTime.toIso8601String(),
    'updateTime': updateTime.toIso8601String(),
    'actualMileage': actualMileage,
    'actualCost': actualCost,
    'startOdometer': startOdometer,
    'endOdometer': endOdometer,
    'returnTime': returnTime?.toIso8601String(),
    'passengers': passengers,
    'approvalRecords': approvalRecords.map((r) => r.toJson()).toList(),
  };

  VehicleModel copyWith({
    String? id,
    String? applicationNo,
    String? applicantId,
    String? applicantName,
    String? deptId,
    String? deptName,
    String? vehicleType,
    String? purpose,
    DateTime? startTime,
    DateTime? endTime,
    String? origin,
    String? destination,
    int? passengerCount,
    String? driver,
    String? licensePlate,
    double? estimatedMileage,
    double? estimatedCost,
    String? reason,
    String? status,
    String? currentApproverId,
    List<String>? approvalChain,
    DateTime? createTime,
    DateTime? updateTime,
    double? actualMileage,
    double? actualCost,
    double? startOdometer,
    double? endOdometer,
    DateTime? returnTime,
    List<String>? passengers,
    List<ApprovalRecord>? approvalRecords,
  }) {
    return VehicleModel(
      id: id ?? this.id,
      applicationNo: applicationNo ?? this.applicationNo,
      applicantId: applicantId ?? this.applicantId,
      applicantName: applicantName ?? this.applicantName,
      deptId: deptId ?? this.deptId,
      deptName: deptName ?? this.deptName,
      vehicleType: vehicleType ?? this.vehicleType,
      purpose: purpose ?? this.purpose,
      startTime: startTime ?? this.startTime,
      endTime: endTime ?? this.endTime,
      origin: origin ?? this.origin,
      destination: destination ?? this.destination,
      passengerCount: passengerCount ?? this.passengerCount,
      driver: driver ?? this.driver,
      licensePlate: licensePlate ?? this.licensePlate,
      estimatedMileage: estimatedMileage ?? this.estimatedMileage,
      estimatedCost: estimatedCost ?? this.estimatedCost,
      reason: reason ?? this.reason,
      status: status ?? this.status,
      currentApproverId: currentApproverId ?? this.currentApproverId,
      approvalChain: approvalChain ?? this.approvalChain,
      createTime: createTime ?? this.createTime,
      updateTime: updateTime ?? this.updateTime,
      actualMileage: actualMileage ?? this.actualMileage,
      actualCost: actualCost ?? this.actualCost,
      startOdometer: startOdometer ?? this.startOdometer,
      endOdometer: endOdometer ?? this.endOdometer,
      returnTime: returnTime ?? this.returnTime,
      passengers: passengers ?? this.passengers,
      approvalRecords: approvalRecords ?? this.approvalRecords,
    );
  }
}
