using CMSMicroservice.Protobuf.Protos.Transactions;
using CMSMicroservice.WebApi.Common.Services;
using CMSMicroservice.Application.TransactionsCQ.Commands.CreateNewTransactions;
using CMSMicroservice.Application.TransactionsCQ.Commands.UpdateTransactions;
using CMSMicroservice.Application.TransactionsCQ.Commands.DeleteTransactions;
using CMSMicroservice.Application.TransactionsCQ.Queries.GetTransactions;
using CMSMicroservice.Application.TransactionsCQ.Queries.GetAllTransactionsByFilter;
using CMSMicroservice.Application.TransactionsCQ.Commands.VerifyTransaction;
using CMSMicroservice.Application.TransactionsCQ.Commands.RefundTransaction;

namespace CMSMicroservice.WebApi.Services;
public class TransactionsService : TransactionsContract.TransactionsContractBase
{
    private readonly IDispatchRequestToCQRS _dispatchRequestToCQRS;

    public TransactionsService(IDispatchRequestToCQRS dispatchRequestToCQRS)
    {
        _dispatchRequestToCQRS = dispatchRequestToCQRS;
    }
    public override async Task<CreateNewTransactionsResponse> CreateNewTransactions(CreateNewTransactionsRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<CreateNewTransactionsRequest, CreateNewTransactionsCommand, CreateNewTransactionsResponse>(request, context);
    }
    public override async Task<Empty> UpdateTransactions(UpdateTransactionsRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<UpdateTransactionsRequest, UpdateTransactionsCommand>(request, context);
    }
    public override async Task<Empty> DeleteTransactions(DeleteTransactionsRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<DeleteTransactionsRequest, DeleteTransactionsCommand>(request, context);
    }
    public override async Task<GetTransactionsResponse> GetTransactions(GetTransactionsRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetTransactionsRequest, GetTransactionsQuery, GetTransactionsResponse>(request, context);
    }
    public override async Task<GetAllTransactionsByFilterResponse> GetAllTransactionsByFilter(GetAllTransactionsByFilterRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetAllTransactionsByFilterRequest, GetAllTransactionsByFilterQuery, GetAllTransactionsByFilterResponse>(request, context);
    }
    
    public override async Task<VerifyTransactionResponse> VerifyTransaction(VerifyTransactionRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<VerifyTransactionRequest, VerifyTransactionCommand, VerifyTransactionResponse>(request, context);
    }
    
    public override async Task<RefundTransactionResponse> RefundTransaction(RefundTransactionRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<RefundTransactionRequest, RefundTransactionCommand, RefundTransactionResponse>(request, context);
    }
    
    // ============= Customer-specific Methods =============
    
    public override async Task<GetCustomerTransactionResponse> GetCustomerTransaction(GetCustomerTransactionRequest request, ServerCallContext context)
    {
        // Mock response for customer transaction
        return new GetCustomerTransactionResponse
        {
            Id = request.Id ?? 1,
            MerchantId = "MERCHANT123",
            Amount = 150000,
            CallbackUrl = "https://mysite.com/callback",
            Description = "خرید محصولات",
            Mobile = "09123456789",
            Email = "customer@example.com",
            RequestStatusCode = 100,
            RequestStatusMessage = "Success",
            Authority = request.Authority ?? "A0000000000000000000000000001234567",
            FeeType = "Payer",
            Fee = 1500,
            Currency = CurrencyEnum.Irr,
            PaymentStatus = true,
            VerificationStatusCode = 101,
            VerificationStatusMessage = "Verified",
            CardHash = "4F8A56B2C1D3E9A7B5C2F1E8D6A9B4C7E3F2A1D5",
            CardPan = "622106******4567",
            RefId = "REF123456789",
            OrderId = "ORDER001",
            Type = TransactionTypeEnum.Real
        };
    }
    
    public override async Task<GetCustomerTransactionsByFilterResponse> GetCustomerTransactionsByFilter(GetCustomerTransactionsByFilterRequest request, ServerCallContext context)
    {
        // Mock response for customer transactions list
        return new GetCustomerTransactionsByFilterResponse
        {
            MetaData = new CMSMicroservice.Protobuf.Protos.MetaData
            {
                CurrentPage = 1,
                TotalPage = 1,
                PageSize = 10,
                TotalCount = 2,
                HasPrevious = false,
                HasNext = false
            },
            Models =
            {
                new GetCustomerTransactionsByFilterResponseModel
                {
                    Id = 1,
                    MerchantId = "MERCHANT123",
                    Amount = 150000,
                    CallbackUrl = "https://mysite.com/callback",
                    Description = "خرید محصولات",
                    Mobile = "09123456789",
                    Email = "customer@example.com",
                    Authority = "A0000000000000000000000000001234567",
                    Fee = 1500,
                    Currency = CurrencyEnum.Irr,
                    PaymentStatus = true,
                    CardHash = "4F8A56B2C1D3E9A7B5C2F1E8D6A9B4C7E3F2A1D5",
                    CardPan = "622106******4567",
                    RefId = "REF123456789",
                    OrderId = "ORDER001",
                    Type = TransactionTypeEnum.Real
                },
                new GetCustomerTransactionsByFilterResponseModel
                {
                    Id = 2,
                    MerchantId = "MERCHANT123",
                    Amount = 75000,
                    CallbackUrl = "https://mysite.com/callback",
                    Description = "تست پرداخت",
                    Mobile = "09123456789",
                    Email = "customer@example.com",
                    Authority = "A0000000000000000000000000001234568",
                    Fee = 750,
                    Currency = CurrencyEnum.Irr,
                    PaymentStatus = false,
                    RefId = "REF123456790",
                    OrderId = "ORDER002",
                    Type = TransactionTypeEnum.Sandbox
                }
            }
        };
    }
    
    public override async Task<CustomerPaymentRequestResponse> CustomerPaymentRequest(CustomerPaymentRequestRequest request, ServerCallContext context)
    {
        // Mock payment gateway response
        return new CustomerPaymentRequestResponse
        {
            PaymentGWUrl = $"https://payment.gateway.com/payment?amount={request.Amount}&callback={request.CallbackUrl}&description={request.Description}"
        };
    }
    
    public override async Task<CustomerPaymentVerificationResponse> CustomerPaymentVerification(CustomerPaymentVerificationRequest request, ServerCallContext context)
    {
        // Mock payment verification response
        bool isSuccessful = request.Status == "OK";
        
        return new CustomerPaymentVerificationResponse
        {
            Id = 12345,
            PaymentStatus = isSuccessful,
            Message = isSuccessful ? "پرداخت با موفقیت انجام شد" : "پرداخت ناموفق",
            RefId = isSuccessful ? "REF123456789" : null,
            OrderId = "ORDER001",
            VerificationStatusCode = isSuccessful ? 101 : 102
        };
    }
}
