import 'dart:typed_data';

import 'package:equatable/equatable.dart';

class AttachmentModel extends Equatable {
  final String? id;
  final DateTime? createdAt;
  final String? customerId;
  final String? operationId;
  final String? ticketId;
  final String? shippingDocumentId;
  final String? noteId;
  final String name;
  final String extension;
  final String? storagePath;
  final int fileSize;
  final Uint8List? localBytes;
  final String companyId;

  const AttachmentModel({
    this.id,
    this.createdAt,
    this.customerId,
    this.operationId,
    this.ticketId,
    this.shippingDocumentId,
    this.noteId,
    required this.name,
    required this.extension,
    this.storagePath,
    required this.fileSize,
    this.localBytes,
    required this.companyId,
  });

  @override
  List<Object?> get props => [
    id,
    createdAt,
    customerId,
    operationId,
    ticketId,
    shippingDocumentId,
    noteId,
    name,
    extension,
    storagePath,
    fileSize,
    localBytes,
    companyId,
  ];

  bool get isLocal => localBytes != null;

  bool get isPdf => extension.toLowerCase().replaceAll('.', '') == 'pdf';

  String get sizeFormatted {
    if (fileSize <= 0) return "0 B";
    const suffixes = ["B", "KB", "MB", "GB", "TB"];
    var i = (fileSize.toString().length - 1) ~/ 3;
    if (i >= suffixes.length) i = suffixes.length - 1;
    double num = fileSize / (1 << (i * 10));
    return "${num.toStringAsFixed(i == 0 ? 0 : 1)} ${suffixes[i]}";
  }

  AttachmentModel copyWith({
    String? id,
    DateTime? createdAt,
    String? customerId,
    String? operationId,
    String? ticketId,
    String? shippingDocumentId,
    String? noteId,
    String? name,
    String? extension,
    String? storagePath,
    int? fileSize,
    Uint8List? localBytes,
    String? companyId,
  }) => AttachmentModel(
    id: id ?? this.id,
    createdAt: createdAt ?? this.createdAt,
    customerId: customerId ?? this.customerId,
    operationId: operationId ?? this.operationId,
    ticketId: ticketId ?? this.ticketId,
    shippingDocumentId: shippingDocumentId ?? this.shippingDocumentId,
    noteId: noteId ?? this.noteId,
    name: name ?? this.name,
    extension: extension ?? this.extension,
    storagePath: storagePath ?? this.storagePath,
    fileSize: fileSize ?? this.fileSize,
    localBytes: localBytes ?? this.localBytes,
    companyId: companyId ?? this.companyId,
  );

  factory AttachmentModel.fromMap(Map<String, dynamic> map) {
    return AttachmentModel(
      id: map['id'] as String,
      createdAt: map['created_at'] != null
          ? DateTime.parse(map['created_at'])
          : null,
      customerId: map['customer_id'] as String?,
      operationId: map['operation_id'] as String?,
      ticketId: map['ticket_id'] as String?,
      shippingDocumentId: map['shipping_document_id'] as String?,
      noteId: map['note_id'] as String?,
      name: map['name'] as String,
      extension: map['extension'] as String,
      storagePath: map['storage_path'] as String?,
      fileSize: map['file_size'] is int
          ? map['file_size']
          : int.tryParse(map['file_size']?.toString() ?? '0') ?? 0,
      companyId: map['company_id'] as String,
    );
  }

  Map<String, dynamic> toMap() {
    return {
      if (id != null) 'id': id,
      'name': name,
      'extension': extension,
      'storage_path': storagePath,
      'customer_id': customerId,
      'operation_id': operationId,
      'ticket_id': ticketId,
      'shipping_document_id': shippingDocumentId,
      'note_id': noteId,
      'file_size': fileSize,
      'company_id': companyId,
    };
  }
}
