import 'package:equatable/equatable.dart';
import 'package:flux/features/attachments/models/attachment_model.dart';

class ShippingDocumentModel extends Equatable {
  final String? id;
  final String companyId;
  final List<String> ticketIds;
  final String providerId;
  final String destinationLocationId;
  final String docNumber;
  final DateTime docDate;
  final int packageCount;
  final double? weight;
  final String shippingReason;
  final String? notes;
  final List<AttachmentModel> attachments;

  const ShippingDocumentModel({
    this.id,
    required this.companyId,
    required this.ticketIds,
    required this.providerId,
    required this.destinationLocationId,
    required this.docNumber,
    required this.docDate,
    this.packageCount = 1,
    this.weight,
    this.shippingReason = 'Riparazione esterna',
    this.notes,
    this.attachments = const [],
  });

  ShippingDocumentModel copyWith({
    String? id,
    String? companyId,
    List<String>? ticketIds,
    String? providerId,
    String? destinationLocationId,
    String? docNumber,
    DateTime? docDate,
    int? packageCount,
    double? weight,
    String? shippingReason,
    String? notes,
    List<AttachmentModel>? attachments,
  }) {
    return ShippingDocumentModel(
      id: id ?? this.id,
      companyId: companyId ?? this.companyId,
      ticketIds: ticketIds ?? this.ticketIds,
      providerId: providerId ?? this.providerId,
      destinationLocationId:
          destinationLocationId ?? this.destinationLocationId,
      docNumber: docNumber ?? this.docNumber,
      docDate: docDate ?? this.docDate,
      packageCount: packageCount ?? this.packageCount,
      weight: weight ?? this.weight,
      shippingReason: shippingReason ?? this.shippingReason,
      notes: notes ?? this.notes,
      attachments: attachments ?? this.attachments,
    );
  }

  factory ShippingDocumentModel.fromMap(Map<String, dynamic> map) {
    return ShippingDocumentModel(
      id: map['id'],
      companyId: map['company_id'],
      ticketIds: List<String>.from(map['ticket_ids']),
      providerId: map['provider_id'],
      destinationLocationId: map['destination_location_id'],
      docNumber: map['doc_number'],
      docDate: DateTime.parse(map['doc_date']),
      packageCount: map['package_count'] ?? 1,
      weight: map['weight'] != null ? (map['weight'] as num).toDouble() : null,
      shippingReason: map['shipping_reason'] ?? 'Riparazione esterna',
      notes: map['notes'],
      attachments:
          (map['attachments'] as List<dynamic>?)
              ?.map((e) => AttachmentModel.fromMap(e))
              .toList() ??
          const [],
    );
  }

  Map<String, dynamic> toMap() {
    return {
      if (id != null) 'id': id,
      'company_id': companyId,
      'ticket_ids': ticketIds,
      'provider_id': providerId,
      'destination_location_id': destinationLocationId,
      'doc_number': docNumber,
      'doc_date': docDate.toIso8601String(),
      'package_count': packageCount,
      'weight': weight,
      'shipping_reason': shippingReason,
      'notes': notes,
    };
  }

  @override
  List<Object?> get props => [
    id,
    docNumber,
    ticketIds,
    providerId,
    destinationLocationId,
    docDate,
    packageCount,
    weight,
    shippingReason,
    notes,
    attachments,
  ];
}
