import '../../domain/entities/tour.dart';

class TourModel extends Tour {
  const TourModel({
    required super.id,
    required super.jobId,
    required super.tourId,
    required super.version,
    required super.state,
    required super.type,
    required super.sort,
    required super.locationId,
    required super.locationCode,
    super.locationCode2,
    super.remark,
    super.menuText,
    required super.modified,
    super.deliveryCode,
    super.locationName,
    super.isCompleted,
    super.pages,
  });

  factory TourModel.fromJson(Map<String, dynamic> json) {
    return TourModel(
      id: json['id'] ?? 0,
      jobId: json['job_id'] ?? 0,
      tourId: json['tour_id'] ?? 0,
      version: json['version'] ?? 0,
      state: json['state'] ?? 0,
      type: json['type'] ?? '',
      sort: json['sort'] ?? 0,
      locationId: json['loc_id'] ?? 0,
      locationCode: json['loc_code'] ?? '',
      locationCode2: json['loc_code_2'],
      remark: json['rem'],
      menuText: json['menu'],
      modified: json['modified'] ?? 0,
      deliveryCode: json['del_code'],
      locationName: json['location_name'],
      isCompleted: json['state'] == 1,
      pages: (json['pages'] as List?)
              ?.map((p) => TourPageModel.fromJson(p))
              .toList() ??
          [],
    );
  }

  factory TourModel.fromMap(Map<String, dynamic> map) {
    return TourModel(
      id: map['id'] ?? 0,
      jobId: map['job_id'] ?? 0,
      tourId: map['tour_id'] ?? 0,
      version: map['version'] ?? 0,
      state: map['state'] ?? 0,
      type: map['type'] ?? '',
      sort: map['sort'] ?? 0,
      locationId: map['loc_id'] ?? 0,
      locationCode: map['loc_code'] ?? '',
      locationCode2: map['loc_code2'],
      remark: map['rem'],
      menuText: map['menu'],
      modified: map['modified'] ?? 0,
      deliveryCode: map['del_code'],
      locationName: map['location_name'],
      isCompleted: map['state'] == 1,
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'job_id': jobId,
      'tour_id': tourId,
      'version': version,
      'state': state,
      'type': type,
      'sort': sort,
      'loc_id': locationId,
      'loc_code': locationCode,
      'loc_code2': locationCode2,
      'rem': remark,
      'menu': menuText,
      'modified': modified,
      'del_code': deliveryCode,
    };
  }

  TourModel copyWithModel({
    int? id,
    int? jobId,
    int? tourId,
    int? version,
    int? state,
    String? type,
    int? sort,
    int? locationId,
    String? locationCode,
    String? locationCode2,
    String? remark,
    String? menuText,
    int? modified,
    String? deliveryCode,
    String? locationName,
    bool? isCompleted,
    List<TourPage>? pages,
  }) {
    return TourModel(
      id: id ?? this.id,
      jobId: jobId ?? this.jobId,
      tourId: tourId ?? this.tourId,
      version: version ?? this.version,
      state: state ?? this.state,
      type: type ?? this.type,
      sort: sort ?? this.sort,
      locationId: locationId ?? this.locationId,
      locationCode: locationCode ?? this.locationCode,
      locationCode2: locationCode2 ?? this.locationCode2,
      remark: remark ?? this.remark,
      menuText: menuText ?? this.menuText,
      modified: modified ?? this.modified,
      deliveryCode: deliveryCode ?? this.deliveryCode,
      locationName: locationName ?? this.locationName,
      isCompleted: isCompleted ?? this.isCompleted,
      pages: pages ?? this.pages,
    );
  }
}

class TourPageModel extends TourPage {
  const TourPageModel({
    required super.id,
    required super.tourId,
    required super.pageNumber,
    required super.pageId,
    required super.type,
    super.code,
    super.label,
    super.pickupCounts,
    super.swapCounts,
  });

  factory TourPageModel.fromJson(Map<String, dynamic> json) {
    Map<String, int> pickupCounts = {};
    Map<String, int> swapCounts = {};

    if (json['pickup'] != null && json['pickup']['cnt'] != null) {
      final cnt = json['pickup']['cnt'] as Map<String, dynamic>;
      pickupCounts = cnt.map((key, value) => MapEntry(key, value as int));
    }

    if (json['swap'] != null && json['swap']['cnt'] != null) {
      final cnt = json['swap']['cnt'] as Map<String, dynamic>;
      swapCounts = cnt.map((key, value) => MapEntry(key, value as int));
    }

    return TourPageModel(
      id: json['id'] ?? 0,
      tourId: json['tour_id'] ?? 0,
      pageNumber: json['page_number'] ?? 0,
      pageId: json['page_id'] ?? '',
      type: json['type'] ?? '',
      code: json['code'],
      label: json['lbl'],
      pickupCounts: pickupCounts,
      swapCounts: swapCounts,
    );
  }

  factory TourPageModel.fromMap(Map<String, dynamic> map) {
    return TourPageModel(
      id: map['id'] ?? 0,
      tourId: map['tour_id'] ?? 0,
      pageNumber: map['page_number'] ?? 0,
      pageId: map['page_id'] ?? '',
      type: map['type'] ?? '',
      code: map['code'],
      label: map['label'],

    );
  }

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'tour_id': tourId,
      'page_number': pageNumber,
      'page_id': pageId,
      'type': type,
      'code': code,
      'label': label,
    };
  }
}
