import 'package:syncrow_web/pages/spaces_management/all_spaces/model/base_tag.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/product_model.dart';
import 'package:syncrow_web/pages/spaces_management/space_model/models/create_space_template_body_model.dart';
import 'package:syncrow_web/pages/spaces_management/space_model/models/tag_body_model.dart';
import 'package:uuid/uuid.dart';

class Tag extends BaseTag {
  Tag({
    String? uuid,
    required String? tag,
    ProductModel? product,
    String? internalId,
    String? location,
  }) : super(
          uuid: uuid,
          tag: tag,
          product: product,
          internalId: internalId,
          location: location,
        );

  factory Tag.fromJson(Map<String, dynamic> json) {
    final String internalId = json['internalId'] ?? const Uuid().v4();

    return Tag(
      //TODO:insure UUId for tag or prodAlloc
      uuid: json['name'] != null ? json['uuid'] : json['tag']?['uuid'] ?? '',
      internalId: internalId,
      tag: json['name'] ?? json['tag']?['name'] ?? '',
      product: json['product'] != null
          ? ProductModel.fromMap(json['product'])
          : null,
    );
  }

  @override
  Tag copyWith({
    String? uuid,
    String? tag,
    ProductModel? product,
    String? location,
    String? internalId,
  }) {
    return Tag(
      uuid: uuid ?? this.uuid,
      tag: tag ?? this.tag,
      product: product ?? this.product,
      location: location ?? this.location,
      internalId: internalId ?? this.internalId,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      if (uuid != null) 'uuid': uuid,
      'name': tag,
      'productUuid': product?.uuid,
      // .toMap(),
    };
  }
}

extension TagModelExtensions on Tag {
  TagBodyModel toTagBodyModel() {
    return TagBodyModel()
      ..uuid = uuid
      ..tag = tag ?? ''
      ..productUuid = product?.uuid;
  }

  CreateTagBodyModel toCreateTagBodyModel() {
    return CreateTagBodyModel()
      ..tag = tag ?? ''
      ..productUuid = product?.uuid;
  }
}
