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

/// Warehouse data model
/// Extends domain entity and adds JSON serialization
/// Matches the API response format from backend
class WarehouseModel extends WarehouseEntity {
  const WarehouseModel({
    required super.id,
    required super.name,
    required super.code,
    super.description,
    required super.isNGWareHouse,
    required super.totalCount,
  });

  /// Create a WarehouseModel from JSON
  ///
  /// JSON format from API:
  /// ```json
  /// {
  ///   "Id": 1,
  ///   "Name": "Kho nguyên vật liệu",
  ///   "Code": "001",
  ///   "Description": "Kho chứa nguyên vật liệu",
  ///   "IsNGWareHouse": false,
  ///   "TotalCount": 8
  /// }
  /// ```
  factory WarehouseModel.fromJson(Map<String, dynamic> json) {
    return WarehouseModel(
      id: json['Id'] ?? 0,
      name: json['Name'] ?? '',
      code: json['Code'] ?? '',
      description: json['Description'],
      isNGWareHouse: json['IsNGWareHouse'] ?? false,
      totalCount: json['TotalCount'] ?? 0,
    );
  }

  /// Convert model to JSON
  Map<String, dynamic> toJson() {
    return {
      'Id': id,
      'Name': name,
      'Code': code,
      'Description': description,
      'IsNGWareHouse': isNGWareHouse,
      'TotalCount': totalCount,
    };
  }

  /// Create from domain entity
  factory WarehouseModel.fromEntity(WarehouseEntity entity) {
    return WarehouseModel(
      id: entity.id,
      name: entity.name,
      code: entity.code,
      description: entity.description,
      isNGWareHouse: entity.isNGWareHouse,
      totalCount: entity.totalCount,
    );
  }

  /// Convert to domain entity
  WarehouseEntity toEntity() {
    return WarehouseEntity(
      id: id,
      name: name,
      code: code,
      description: description,
      isNGWareHouse: isNGWareHouse,
      totalCount: totalCount,
    );
  }

  /// Create a copy with modified fields
  @override
  WarehouseModel copyWith({
    int? id,
    String? name,
    String? code,
    String? description,
    bool? isNGWareHouse,
    int? totalCount,
  }) {
    return WarehouseModel(
      id: id ?? this.id,
      name: name ?? this.name,
      code: code ?? this.code,
      description: description ?? this.description,
      isNGWareHouse: isNGWareHouse ?? this.isNGWareHouse,
      totalCount: totalCount ?? this.totalCount,
    );
  }

  @override
  String toString() {
    return 'WarehouseModel(id: $id, name: $name, code: $code, totalCount: $totalCount)';
  }
}
