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

class CustomerModel extends Equatable {
  final String? id; // Bigint in SQL
  final DateTime? createdAt;
  final String name;
  final String phoneNumber;
  final String email;
  final String note;
  final DateTime? lastContactDate;
  final bool doNotDisturb;
  final String companyId; // UUID
  final bool isActive;
  final List<AttachmentModel> attachments;
  final bool isBusiness;

  const CustomerModel({
    this.id,
    this.createdAt,
    required this.name,
    required this.phoneNumber,
    required this.email,
    required this.note,
    this.lastContactDate,
    this.doNotDisturb = false,
    required this.companyId,
    this.isActive = true,
    this.attachments = const [],
    this.isBusiness = false,
  });

  @override
  List<Object?> get props => [
    id,
    createdAt,
    name,
    phoneNumber,
    email,
    note,
    lastContactDate,
    doNotDisturb,
    companyId,
    isActive,
    attachments,
    isBusiness,
  ];

  factory CustomerModel.empty() => CustomerModel(
    name: '',
    phoneNumber: '',
    email: '',
    note: '',
    companyId:
        '', // Dovrebbe essere sempre fornito, ma lasciamo vuoto per sicurezza
  );

  CustomerModel copyWith({
    String? id,
    DateTime? createdAt,
    String? name,
    String? phoneNumber,
    String? email,
    String? note,
    DateTime? lastContactDate,
    bool? doNotDisturb,
    String? companyId,
    bool? isActive,
    List<AttachmentModel>? attachments,
    bool? isBusiness,
  }) {
    return CustomerModel(
      id: id ?? this.id,
      createdAt: createdAt ?? this.createdAt,
      name: name ?? this.name,
      phoneNumber: phoneNumber ?? this.phoneNumber,
      email: email ?? this.email,
      note: note ?? this.note,
      lastContactDate: lastContactDate ?? this.lastContactDate,
      doNotDisturb: doNotDisturb ?? this.doNotDisturb,
      companyId: companyId ?? this.companyId,
      isActive: isActive ?? this.isActive,
      attachments: attachments ?? this.attachments,
      isBusiness: isBusiness ?? this.isBusiness,
    );
  }

  factory CustomerModel.fromMap(Map<String, dynamic> map) {
    return CustomerModel(
      id: map['id'] as String,
      createdAt: map['created_at'] != null
          ? DateTime.parse(map['created_at'])
          : null,
      name: (map['name'] as String).myFormat(),
      phoneNumber: map['phone_number'],
      email: map['email'],
      note: map['note'] ?? '',
      lastContactDate: map['last_contact_date'] != null
          ? DateTime.parse(map['last_contact_date'])
          : null,
      doNotDisturb: map['do_not_disturb'] ?? false,
      companyId: map['company_id'] as String,
      isActive: map['is_active'] ?? true,
      attachments:
          (map['attachment'] as List?)
              ?.map((x) => AttachmentModel.fromMap(x))
              .toList() ??
          const [],
      isBusiness: map['is_business'] as bool? ?? false,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      if (id != null) 'id': id,
      'name': name.toLowerCase().trim(),
      'phone_number': phoneNumber,
      'email': email.toLowerCase().trim(),
      'note': note,
      if (lastContactDate != null)
        'last_contact_date': lastContactDate!.toIso8601String(),
      'do_not_disturb': doNotDisturb,
      'company_id': companyId,
      'is_active': isActive,
      'is_business': isBusiness,
    };
  }
}
