import 'dart:convert';
import 'package:nearledaily/service/dio.dart';
import 'package:nearledaily/helper/logger.dart';
import '../../../modules/product/product.dart';
import '../../repository/varient/varient_repo.dart';

class ProductVariantProvider implements ProductVariantRepository {
  final CustomDio customDio = CustomDio();

  @override
  Future<List<Product>?> getProductVariant({
    required int tenantId,
    required int variantId,
  }) async {
    final url =
        "https://fiesta.nearle.app/live/api/v1/mob/products/getproductbyvariant?tenantid=$tenantId&variantid=$variantId";
    logger.i("GET ProductVariant URL: $url");

    try {
      final response = await customDio.getData(url);

      if (response != null &&
          response['code'] == 200 &&
          response['details'] != null &&
          response['details'].isNotEmpty) {
        // Map JSON using new Product.fromJson factory
        final products = (response['details'] as List)
            .map((e) => Product.fromJson(e as Map<String, dynamic>))
            .toList();

        logger.i("GET ProductVariant Response: ${jsonEncode(products)}");
        return products;
      } else {
        logger.w("No product variants found for variantId $variantId");
        return [];
      }
    } catch (e) {
      logger.e("Error in getProductVariant: $e");
      return [];
    }
  }
}
