import 'dart:developer';

import 'package:data_channel/data_channel.dart';
import 'package:injectable/injectable.dart';

import '../../../common/api/api_client.dart';
import '../../../common/api/api_failure.dart';
import '../../../common/function/app_function.dart';
import '../../../common/url/api_path.dart';
import '../../../domain/category/category.dart';
import '../category_dtos.dart';

@injectable
class CategoryRemoteDataProvider {
  final ApiClient _apiClient;
  final String _logName = 'CategoryRemoteDataProvider';

  CategoryRemoteDataProvider(this._apiClient);

  Future<DC<CategoryFailure, List<CategoryDto>>> fetch({
    int page = 1,
    int limit = 20,
    bool isActive = true,
  }) async {
    try {
      final response = await _apiClient.get(
        ApiPath.category,
        params: {'page': page, 'limit': limit, 'is_active': isActive},
        headers: getAuthorizationHeader(),
      );

      if (response.data['data'] == null) {
        return DC.error(CategoryFailure.empty());
      }

      final dto = (response.data['data']['categories'] as List)
          .map((item) => CategoryDto.fromJson(item))
          .toList();

      return DC.data(dto);
    } on ApiFailure catch (e, s) {
      log('fetchCategoryError', name: _logName, error: e, stackTrace: s);
      return DC.error(CategoryFailure.serverError(e));
    }
  }
}
