// src/SIGCM.Infrastructure/Services/MercadoPagoService.cs
using MercadoPago.Client.Preference;
using MercadoPago.Config;
using MercadoPago.Resource.Preference;
using SIGCM.Domain.Entities;

namespace SIGCM.Infrastructure.Services;

public class MercadoPagoService
{
    private readonly string _accessToken;
    private readonly string _successUrl;
    private readonly string _failureUrl;
    private readonly string _notificationUrl;

    public MercadoPagoService(string accessToken, string successUrl, string failureUrl, string notificationUrl)
    {
        _accessToken = accessToken;
        _successUrl = successUrl;
        _failureUrl = failureUrl;
        _notificationUrl = notificationUrl;

        MercadoPagoConfig.AccessToken = _accessToken;
    }

    public async Task<object> CreatePreferenceAsync(Listing listing, decimal totalAmount)
    {
        /* 
        // IMPLEMENTACIÓN REAL (COMENTADA PARA FASE FINAL)
        public async Task<Preference> CreatePreferenceAsync(Listing listing, decimal totalAmount)
        {
        var request = new PreferenceRequest
        {
            Items = new List<PreferenceItemRequest>
            {
                new PreferenceItemRequest
                {
                    Id = listing.Id.ToString(),
                    Title = $"Publicación de Aviso: {listing.Title}",
                    Quantity = 1,
                    CurrencyId = "ARS",
                    UnitPrice = totalAmount
                }
            },
            Payer = new PreferencePayerRequest
            {
                Email = "test_user_123@testuser.com", // En producción usar email del cliente
            },
            BackUrls = new PreferenceBackUrlsRequest
            {
                Success = _successUrl,
                Failure = _failureUrl,
                Pending = _failureUrl
            },
            AutoReturn = "approved",
            ExternalReference = listing.Id.ToString(),
            NotificationUrl = _notificationUrl,
            StatementDescriptor = "DIARIO EL DIA"
        };

        var client = new PreferenceClient();
        return await client.CreateAsync(request);
        */
        // SIMULACIÓN PARA DESARROLLO
        await Task.Delay(500); // Simulamos latencia de red
        return new
        {
            Id = "MOCK_PREFERENCE_ID_12345",
            InitPoint = "#",
            SandboxInitPoint = "#"
        };
    }
}
