import 'package:dio/dio.dart';
import 'package:niogu_ecommerce_v1/core/errors/exceptions.dart';
import 'package:niogu_ecommerce_v1/core/models/tenant_model.dart';
import 'package:niogu_ecommerce_v1/core/utils/log_message.dart';
import 'package:niogu_ecommerce_v1/features/home/data/datasource/home_remote_datasource_impl.dart';
import 'package:niogu_ecommerce_v1/features/home/domain/entities/home.dart';
import 'package:niogu_ecommerce_v1/features/home/domain/repositories/i_home_repository.dart';

class HomeRepositoryImpl implements IHomeRepository {
  final IHomeRemoteDatasource _datasource;

  HomeRepositoryImpl(this._datasource);

  @override
  Future<TenantModel?> fetchConfiguration() async {
    try {
      final response = await _datasource.fetchConfiguration();

      return response.data;
    } on DioException catch (e, st) {
      LogMessage.log.e(e.toString(), error: e, stackTrace: st);

      throw ServerException("Terjadi kesalahan koneksi");
    }
  }

  @override
  Future<Home?> fetchHome() async {
    try {
      final response = await _datasource.fetchHome();

      final data = response.data;

      if (response.status != 'success' || data == null) return null;

      return data.toHome();
    } on DioException catch (e, st) {
      LogMessage.log.e(e.toString(), error: e, stackTrace: st);

      throw ServerException("Terjadi kesalahan koneksi");
    }
  }

  @override
  Future<List<ProductItem>> fetchProductByName(String name) async {
    try {
      final response = await _datasource.fetchProductByName(name);

      final data = response.data;

      if (response.status != 'success' || data == null) return [];

      return data.map((product) => product.toProductItem()).toList();
    } on DioException catch (e, st) {
      LogMessage.log.e(e.toString(), error: e, stackTrace: st);

      throw ServerException("Terjadi kesalahan koneksi");
    }
  }

  @override
  Future<List<ProductItem>> fetchProductCategories(String categoryId) async {
    try {
      final response = await _datasource.fetchProductCategories(categoryId);

      final data = response.data;

      if (response.status != 'success' || data == null) return [];

      return data.map((product) => product.toProductItem()).toList();
    } on DioException catch (e, st) {
      LogMessage.log.e(e.toString(), error: e, stackTrace: st);

      throw ServerException("Terjadi kesalahan koneksi");
    }
  }

  @override
  Future<List<OtherOutlet>> fetchOutlets() async {
    try {
      final response = await _datasource.fetchOutlets();

      final data = response.data;

      if (response.status != 'success' || data == null) return [];

      return data.map((outlet) => outlet.toOtherOutlet()).toList();
    } on DioException catch (e, st) {
      LogMessage.log.e(e.toString(), error: e, stackTrace: st);

      throw ServerException("Terjadi kesalahan koneksi");
    }
  }

  @override
  Future<MainOutlet?> fetchMainOutlet() async {
    try {
      final response = await _datasource.fetchMainOutlet();

      final data = response.data;

      if (response.status != 'success' || data == null) return null;

      return data.toMainOutlet();
    } on DioException catch (e, st) {
      LogMessage.log.e(e.toString(), error: e, stackTrace: st);

      throw ServerException("Terjadi kesalahan koneksi");
    }
  }
}
