class PrintModel {
  int? id;
  final String code;
  final String name;
  final String address;
  final String paper;
  final String type;
  final DateTime? createdAt;
  final DateTime? updatedAt;

  PrintModel({
    this.id,
    required this.code,
    required this.name,
    required this.address,
    required this.paper,
    required this.type,
    this.createdAt,
    this.updatedAt,
  });

  // Factory constructor from map (updated)
  factory PrintModel.fromMap(Map<String, dynamic> map) {
    return PrintModel(
      id: map['id'] as int?,
      code: map['code'] as String,
      name: map['name'] as String,
      address: map['address'] as String,
      paper: map['paper'] as String,
      type: map['type'] as String,
      createdAt: map['created_at'] != null
          ? DateTime.tryParse(map['created_at'] as String)
          : null,
      updatedAt: map['updated_at'] != null
          ? DateTime.tryParse(map['updated_at'] as String)
          : null,
    );
  }

  // Convert to map for database insertion (without id, with timestamps)
  Map<String, dynamic> toMapForInsert() {
    final now = DateTime.now().toIso8601String();
    return {
      'code': code,
      'name': name,
      'address': address,
      'paper': paper,
      'type': type,
      'created_at': now,
      'updated_at': now,
    };
  }

  // Convert to map for database update (without id and created_at)
  Map<String, dynamic> toMapForUpdate() {
    return {
      'code': code,
      'name': name,
      'address': address,
      'paper': paper,
      'type': type,
      'updated_at': DateTime.now().toIso8601String(),
    };
  }

  // Convert to complete map (original method, enhanced)
  Map<String, dynamic> toMap() {
    return {
      if (id != null) 'id': id,
      'code': code,
      'name': name,
      'address': address,
      'paper': paper,
      'type': type,
      if (createdAt != null) 'created_at': createdAt!.toIso8601String(),
      if (updatedAt != null) 'updated_at': updatedAt!.toIso8601String(),
    };
  }

  // Copy with method for creating modified instances
  PrintModel copyWith({
    int? id,
    String? code,
    String? name,
    String? address,
    String? paper,
    String? type,
    DateTime? createdAt,
    DateTime? updatedAt,
  }) {
    return PrintModel(
      id: id ?? this.id,
      code: code ?? this.code,
      name: name ?? this.name,
      address: address ?? this.address,
      paper: paper ?? this.paper,
      type: type ?? this.type,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
    );
  }

  // Equality and hashCode
  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is PrintModel &&
        other.id == id &&
        other.code == code &&
        other.name == name &&
        other.address == address &&
        other.paper == paper &&
        other.type == type;
  }

  @override
  int get hashCode {
    return Object.hash(id, code, name, address, paper, type);
  }

  // String representation for debugging (matches datasource logging)
  @override
  String toString() {
    return 'PrintModel(id: $id, code: $code, name: $name, address: $address, paper: $paper, type: $type, createdAt: $createdAt, updatedAt: $updatedAt)';
  }

  // Validation methods
  bool get isValid {
    return code.isNotEmpty &&
        name.isNotEmpty &&
        address.isNotEmpty &&
        paper.isNotEmpty &&
        type.isNotEmpty;
  }

  String? get validationError {
    if (code.isEmpty) return 'Printer code cannot be empty';
    if (name.isEmpty) return 'Printer name cannot be empty';
    if (address.isEmpty) return 'Printer address cannot be empty';
    if (paper.isEmpty) return 'Paper size cannot be empty';
    if (type.isEmpty) return 'Printer type cannot be empty';
    return null;
  }
}
