import 'dart:developer';

import 'package:syncrow_web/pages/spaces_management/all_spaces/model/tag.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/space_template_model.dart';
import 'package:syncrow_web/services/api/http_service.dart';
import 'package:syncrow_web/utils/constants/api_const.dart';

class SpaceModelManagementApi {
  Future<List<SpaceTemplateModel>> listSpaceModels(
      {required String projectId, int page = 1}) async {
    try {
      // final response = await HTTPService().get(
      //   path: ApiEndpoints.listSpaceModels.replaceAll('{projectId}', projectId),
      //   queryParameters: {'page': page},
      //   expectedResponseModel: (json) {
      //     List<dynamic> jsonData = json['data'];
      //     return jsonData.map((jsonItem) {
      //       return SpaceTemplateModel.fromJson(jsonItem);
      //     }).toList();
      //   },
      // );
      return [];
      // response;
    } catch (e) {
      log(e.toString());
      return [];
    }
  }

  Future<SpaceTemplateModel?> createSpaceModel(
      CreateSpaceTemplateBodyModel spaceModel, String projectId) async {
    final response = await HTTPService().post(
      path: ApiEndpoints.createSpaceModel.replaceAll('{projectId}', projectId),
      showServerMessage: true,
      body: spaceModel.toJson(),
      expectedResponseModel: (json) {
        return SpaceTemplateModel.fromJson(json['data']);
      },
    );
    return response;
  }

  Future<String?> updateSpaceModel(CreateSpaceTemplateBodyModel spaceModel,
      String spaceModelUuid, String projectId) async {
    final response = await HTTPService().put(
      path: ApiEndpoints.updateSpaceModel
          .replaceAll('{projectId}', projectId)
          .replaceAll('{spaceModelUuid}', spaceModelUuid),
      body: spaceModel.toJson(),
      expectedResponseModel: (json) {
        return json['message'];
      },
    );
    return response;
  }

  Future<SpaceTemplateModel?> getSpaceModel(
      String spaceModelUuid, String projectId) async {
    final response = await HTTPService().get(
      path: ApiEndpoints.getSpaceModel
          .replaceAll('{projectId}', projectId)
          .replaceAll('{spaceModelUuid}', spaceModelUuid),
      showServerMessage: true,
      expectedResponseModel: (json) {
        return SpaceTemplateModel.fromJson(json['data']);
      },
    );
    return response;
  }

  Future linkSpaceModel(
      {required String spaceModelUuid,
      required String projectId,
      required List<String> spaceUuids,
      required bool isOverWrite}) async {
    final response = await HTTPService().post(
      path: ApiEndpoints.linkSpaceModel
          .replaceAll('{projectId}', projectId)
          .replaceAll('{spaceModelUuid}', spaceModelUuid),
      showServerMessage: true,
      body: {"spaceUuids": spaceUuids, "overwrite": isOverWrite},
      expectedResponseModel: (json) {
        return json;
      },
    );
    return response;
  }

  Future validateSpaceModel(String projectId, List<String> spaceUuids) async {
    final response = await HTTPService().post(
        path: ApiEndpoints.validateSpaceModel
            .replaceAll('{projectId}', projectId),
        showServerMessage: true,
        body: {"spacesUuids": spaceUuids},
        expectedResponseModel: (json) {
          return json;
        });
    return response;
  }

  Future<bool> deleteSpaceModel(String spaceModelUuid, String projectId) async {
    final response = await HTTPService().delete(
      path: ApiEndpoints.getSpaceModel
          .replaceAll('{projectId}', projectId)
          .replaceAll('{spaceModelUuid}', spaceModelUuid),
      showServerMessage: true,
      expectedResponseModel: (json) {
        return json['success'] ?? false;
      },
    );
    return response;
  }

  Future<List<Tag>> listTags({required String projectId}) async {
    final response = await HTTPService().get(
      path: ApiEndpoints.listTags.replaceAll('{projectId}', projectId),
      expectedResponseModel: (json) {
        List<dynamic> jsonData = json['data'];
        return jsonData.map((jsonItem) {
          return Tag.fromJson(jsonItem);
        }).toList();
      },
    );
    return response;
  }
}
