import 'package:equatable/equatable.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/tag.dart';
import 'package:syncrow_web/pages/spaces_management/space_model/models/subspace_template_model.dart';
import 'package:syncrow_web/pages/spaces_management/space_model/models/tag_update_model.dart';
import 'package:syncrow_web/utils/constants/action_enum.dart';
import 'package:uuid/uuid.dart';

class SpaceTemplateModel extends Equatable {
  String? uuid;
  String modelName;
  List<SubspaceTemplateModel>? subspaceModels;
  final List<Tag>? tags;
  String internalId;
  DateTime? createdAt;

  @override
  List<Object?> get props => [modelName, subspaceModels, tags];

  SpaceTemplateModel({
    this.uuid,
    String? internalId,
    required this.modelName,
    this.subspaceModels,
    this.tags,
    this.createdAt,
  }) : internalId = internalId ?? const Uuid().v4();
  factory SpaceTemplateModel.fromJson(Map<String, dynamic> json) {
    final String internalId = json['internalId'] ?? const Uuid().v4();

    return SpaceTemplateModel(
      uuid: json['uuid'] ?? '',
      createdAt: json['createdAt'] != null
          ? DateTime.tryParse(json['createdAt']) 
          : null,
      internalId: internalId,
      modelName: json['modelName'] ?? '',
      subspaceModels: (json['subspaceModels'] as List<dynamic>?)
              ?.where((e) => e is Map<String, dynamic>)
              .map((e) =>
                  SubspaceTemplateModel.fromJson(e as Map<String, dynamic>))
              .toList() ??
          [],
      tags: (json['tags'] as List<dynamic>?)
              ?.where((item) => item is Map<String, dynamic>) // Validate type
              .map((item) => Tag.fromJson(item as Map<String, dynamic>))
              .toList() ??
          [],
    );
  }
  SpaceTemplateModel copyWith({
    String? uuid,
    String? modelName,
    List<SubspaceTemplateModel>? subspaceModels,
    List<Tag>? tags,
    String? internalId,
  }) {
    return SpaceTemplateModel(
      uuid: uuid ?? this.uuid,
      modelName: modelName ?? this.modelName,
      subspaceModels: subspaceModels ?? this.subspaceModels,
      tags: tags ?? this.tags,
      internalId: internalId ?? this.internalId,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'uuid': uuid,
      'modelName': modelName,
      'subspaceModels': subspaceModels?.map((e) => e.toJson()).toList(),
      'tags': tags?.map((e) => e.toJson()).toList(),
    };
  }
}

class UpdateSubspaceTemplateModel {
  final String? uuid;
  final Action action;
  final String? subspaceName;
  final List<TagModelUpdate>? tags;

  UpdateSubspaceTemplateModel({
    required this.action,
    this.uuid,
    this.subspaceName,
    this.tags,
  });

  factory UpdateSubspaceTemplateModel.fromJson(Map<String, dynamic> json) {
    return UpdateSubspaceTemplateModel(
      action: ActionExtension.fromValue(json['action']),
      uuid: json['uuid'] ?? '',
      subspaceName: json['subspaceName'] ?? '',
      tags: (json['tags'] as List)
          .map((item) => TagModelUpdate.fromJson(item))
          .toList(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'action': action.value,
      'uuid': uuid,
      'subspaceName': subspaceName,
      'tags': tags?.map((e) => e.toJson()).toList() ?? [],
    };
  }
}

extension SpaceTemplateExtensions on SpaceTemplateModel {
  List<String> listAllTagValues() {
    final List<String> tagValues = [];

    if (tags != null) {
      tagValues.addAll(
          tags!.map((tag) => tag.tag ?? '').where((tag) => tag.isNotEmpty));
    }

    if (subspaceModels != null) {
      for (final subspace in subspaceModels!) {
        if (subspace.tags != null) {
          tagValues.addAll(
            subspace.tags!
                .map((tag) => tag.tag ?? '')
                .where((tag) => tag.isNotEmpty),
          );
        }
      }
    }

    return tagValues;
  }
}
