import 'package:dio/dio.dart';
import 'package:niogu_ecommerce_v1/core/constant/app_url.dart';
import 'package:niogu_ecommerce_v1/core/models/api_response_model.dart';
import 'package:niogu_ecommerce_v1/core/models/tenant_model.dart';
import 'package:niogu_ecommerce_v1/core/system/system_setting.dart';
import 'package:niogu_ecommerce_v1/features/home/data/models/home_model.dart';

abstract class IHomeRemoteDatasource {
  Future<ApiResponseModel<TenantModel>> fetchConfiguration();

  Future<ApiResponseModel<HomeResponse>> fetchHome();

  Future<ApiResponseModel<List<ProductItemResponse>>> fetchProductByName(
    String name,
  );

  Future<ApiResponseModel<List<ProductItemResponse>>> fetchProductCategories(
    String categoryId,
  );

  Future<ApiResponseModel<List<OtherOutletResponse>>> fetchOutlets();

  Future<ApiResponseModel<MainOutletResponse>> fetchMainOutlet();

}

class HomeRemoteDatasourceImpl implements IHomeRemoteDatasource {
  final Dio _dio;

  HomeRemoteDatasourceImpl(this._dio);

  @override
  Future<ApiResponseModel<TenantModel>> fetchConfiguration() async {
    final response = await _dio.get(AppUrl.TENANT);

    return ApiResponseModel<TenantModel>.fromJson(
      response.data,
      (json) => TenantModel.fromJson(json as Map<String, dynamic>),
    );
  }

  @override
  Future<ApiResponseModel<HomeResponse>> fetchHome() async {
    final currentOutletId = await SystemSetting.getCurrentOutletId();

    final response = await _dio.get("${AppUrl.HOME}/$currentOutletId");

    return ApiResponseModel<HomeResponse>.fromJson(
      response.data,
      (json) => HomeResponse.fromJson(json as Map<String, dynamic>),
    );
  }

  @override
  Future<ApiResponseModel<List<ProductItemResponse>>> fetchProductByName(
    String name,
  ) async {
    final currentOutletId = await SystemSetting.getCurrentOutletId();

    final response = await _dio.get(
      "${AppUrl.PRODUCTS}/search/$currentOutletId?name=$name",
    );

    return ApiResponseModel<List<ProductItemResponse>>.fromJson(
      response.data,
      (json) => (json as List)
          .map(
            (item) =>
                ProductItemResponse.fromJson(item as Map<String, dynamic>),
          )
          .toList(),
    );
  }

  @override
  Future<ApiResponseModel<List<ProductItemResponse>>> fetchProductCategories(
    String categoryId,
  ) async {
    final currentOutletId = await SystemSetting.getCurrentOutletId();

    final response = await _dio.get(
      "${AppUrl.PRODUCTS}/categories/$currentOutletId/$categoryId",
    );

    return ApiResponseModel<List<ProductItemResponse>>.fromJson(
      response.data,
      (json) => (json as List)
          .map(
            (item) =>
                ProductItemResponse.fromJson(item as Map<String, dynamic>),
          )
          .toList(),
    );
  }

  @override
  Future<ApiResponseModel<List<OtherOutletResponse>>> fetchOutlets() async {
    final response = await _dio.get(AppUrl.OUTLETS);

    return ApiResponseModel<List<OtherOutletResponse>>.fromJson(
      response.data,
      (json) => (json as List)
          .map(
            (item) =>
                OtherOutletResponse.fromJson(item as Map<String, dynamic>),
          )
          .toList(),
    );
  }

  @override
  Future<ApiResponseModel<MainOutletResponse>> fetchMainOutlet() async {
    final response = await _dio.get("${AppUrl.OUTLETS}/main");

    return ApiResponseModel.fromJson(
      response.data,
      (json) => MainOutletResponse.fromJson(json as Map<String, dynamic>),
    );
  }
}
