import 'package:equatable/equatable.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/products/domain/models/product.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/tags/domain/models/tag.dart';

class SpaceDetailsModel extends Equatable {
  final String uuid;
  final String spaceName;
  final String icon;
  final List<ProductAllocation> productAllocations;
  final List<Subspace> subspaces;

  const SpaceDetailsModel({
    required this.uuid,
    required this.spaceName,
    required this.icon,
    required this.productAllocations,
    required this.subspaces,
  });

  factory SpaceDetailsModel.fromJson(Map<String, dynamic> json) {
    return SpaceDetailsModel(
      uuid: json['uuid'] as String,
      spaceName: json['spaceName'] as String,
      icon: json['icon'] as String,
      productAllocations: (json['productAllocations'] as List)
          .map((e) => ProductAllocation.fromJson(e as Map<String, dynamic>))
          .toList(),
      subspaces: (json['subspaces'] as List)
          .map((e) => Subspace.fromJson(e as Map<String, dynamic>))
          .toList(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'uuid': uuid,
      'spaceName': spaceName,
      'icon': icon,
      'productAllocations': productAllocations.map((e) => e.toJson()).toList(),
      'subspaces': subspaces.map((e) => e.toJson()).toList(),
    };
  }

  @override
  List<Object?> get props => [uuid, spaceName, icon, productAllocations, subspaces];
}

class ProductAllocation extends Equatable {
  final Product product;
  final Tag tag;
  final String? location;

  const ProductAllocation({
    required this.product,
    required this.tag,
    this.location,
  });

  factory ProductAllocation.fromJson(Map<String, dynamic> json) {
    return ProductAllocation(
      product: Product.fromJson(json['product'] as Map<String, dynamic>),
      tag: Tag.fromJson(json['tag'] as Map<String, dynamic>),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'product': product.toJson(),
      'tag': tag.toJson(),
    };
  }

  @override
  List<Object?> get props => [product, tag];
}

class Subspace extends Equatable {
  final String uuid;
  final String name;
  final List<ProductAllocation> productAllocations;

  const Subspace({
    required this.uuid,
    required this.name,
    required this.productAllocations,
  });

  factory Subspace.fromJson(Map<String, dynamic> json) {
    return Subspace(
      uuid: json['uuid'] as String,
      name: json['name'] as String,
      productAllocations: (json['productAllocations'] as List)
          .map((e) => ProductAllocation.fromJson(e as Map<String, dynamic>))
          .toList(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'uuid': uuid,
      'name': name,
      'productAllocations': productAllocations.map((e) => e.toJson()).toList(),
    };
  }

  @override
  List<Object?> get props => [uuid, name, productAllocations];
}
