import 'dart:developer';

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

import '../../../domain/analytic/analytic.dart';
import '../datasources/remote_data_provider.dart';

@Injectable(as: IAnalyticRepository)
class AnalyticRepository implements IAnalyticRepository {
  final AnalyticRemoteDataProvider _dataProvider;

  final _logName = 'AnalyticRepository';

  AnalyticRepository(this._dataProvider);

  @override
  Future<Either<AnalyticFailure, DashboardAnalytic>> getDashboard({
    required DateTime dateFrom,
    required DateTime dateTo,
  }) async {
    try {
      final result = await _dataProvider.fetchDashboard(
        dateFrom: dateFrom,
        dateTo: dateTo,
      );

      if (result.hasError) {
        return left(result.error!);
      }

      final dashboard = result.data!.toDomain();

      return right(dashboard);
    } catch (e) {
      log('getDashboardError', name: _logName, error: e);
      return left(const AnalyticFailure.unexpectedError());
    }
  }

  @override
  Future<Either<AnalyticFailure, SalesAnalytic>> getSales({
    required DateTime dateFrom,
    required DateTime dateTo,
  }) async {
    try {
      final result = await _dataProvider.fetchSales(
        dateFrom: dateFrom,
        dateTo: dateTo,
      );

      if (result.hasError) {
        return left(result.error!);
      }

      final sales = result.data!.toDomain();

      return right(sales);
    } catch (e) {
      log('getSalesError', name: _logName, error: e);
      return left(const AnalyticFailure.unexpectedError());
    }
  }

  @override
  Future<Either<AnalyticFailure, ProductAnalytic>> getProducts({
    required DateTime dateFrom,
    required DateTime dateTo,
  }) async {
    try {
      final result = await _dataProvider.fetchProducts(
        dateFrom: dateFrom,
        dateTo: dateTo,
      );

      if (result.hasError) {
        return left(result.error!);
      }

      final products = result.data!.toDomain();

      return right(products);
    } catch (e) {
      log('getProductsError', name: _logName, error: e);
      return left(const AnalyticFailure.unexpectedError());
    }
  }

  @override
  Future<Either<AnalyticFailure, PaymentMethodAnalytic>> getPaymentMethod({
    required DateTime dateFrom,
    required DateTime dateTo,
  }) async {
    try {
      final result = await _dataProvider.fetchPaymentMethod(
        dateFrom: dateFrom,
        dateTo: dateTo,
      );

      if (result.hasError) {
        return left(result.error!);
      }

      final paymentMethod = result.data!.toDomain();

      return right(paymentMethod);
    } catch (e) {
      log('getPaymentMethodError', name: _logName, error: e);
      return left(const AnalyticFailure.unexpectedError());
    }
  }

  @override
  Future<Either<AnalyticFailure, ProfitLossAnalytic>> getProfitLoss({
    required DateTime dateFrom,
    required DateTime dateTo,
  }) async {
    try {
      final result = await _dataProvider.fetchProfitLoss(
        dateFrom: dateFrom,
        dateTo: dateTo,
      );

      if (result.hasError) {
        return left(result.error!);
      }

      final profitLoss = result.data!.toDomain();

      return right(profitLoss);
    } catch (e) {
      log('getProfitLossError', name: _logName, error: e);
      return left(const AnalyticFailure.unexpectedError());
    }
  }

  @override
  Future<Either<AnalyticFailure, CategoryAnalytic>> getCategories({
    required DateTime dateFrom,
    required DateTime dateTo,
  }) async {
    try {
      final result = await _dataProvider.fetchCategories(
        dateFrom: dateFrom,
        dateTo: dateTo,
      );

      if (result.hasError) {
        return left(result.error!);
      }

      final categories = result.data!.toDomain();

      return right(categories);
    } catch (e) {
      log('getCategoriesError', name: _logName, error: e);
      return left(const AnalyticFailure.unexpectedError());
    }
  }

  @override
  Future<Either<AnalyticFailure, InventoryAnalytic>> getInventory({
    required DateTime dateFrom,
    required DateTime dateTo,
  }) async {
    try {
      final result = await _dataProvider.fetchInventory(
        dateFrom: dateFrom,
        dateTo: dateTo,
      );

      if (result.hasError) {
        return left(result.error!);
      }

      final inventory = result.data!.toDomain();

      return right(inventory);
    } catch (e) {
      log('getInventoryError', name: _logName, error: e);
      return left(const AnalyticFailure.unexpectedError());
    }
  }
}
