using CMSMicroservice.Protobuf.Protos.UserWallet;
using CMSMicroservice.WebApi.Common.Services;
using CMSMicroservice.Application.UserWalletCQ.Commands.CreateNewUserWallet;
using CMSMicroservice.Application.UserWalletCQ.Commands.UpdateUserWallet;
using CMSMicroservice.Application.UserWalletCQ.Commands.DeleteUserWallet;
using CMSMicroservice.Application.UserWalletCQ.Queries.GetUserWallet;
using CMSMicroservice.Application.UserWalletCQ.Queries.GetAllUserWalletByFilter;
namespace CMSMicroservice.WebApi.Services;
public class UserWalletService : UserWalletContract.UserWalletContractBase
{
    private readonly IDispatchRequestToCQRS _dispatchRequestToCQRS;

    public UserWalletService(IDispatchRequestToCQRS dispatchRequestToCQRS)
    {
        _dispatchRequestToCQRS = dispatchRequestToCQRS;
    }
    public override async Task<CreateNewUserWalletResponse> CreateNewUserWallet(CreateNewUserWalletRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<CreateNewUserWalletRequest, CreateNewUserWalletCommand, CreateNewUserWalletResponse>(request, context);
    }
    public override async Task<Empty> UpdateUserWallet(UpdateUserWalletRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<UpdateUserWalletRequest, UpdateUserWalletCommand>(request, context);
    }
    public override async Task<Empty> DeleteUserWallet(DeleteUserWalletRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<DeleteUserWalletRequest, DeleteUserWalletCommand>(request, context);
    }
    public override async Task<GetUserWalletResponse> GetUserWallet(GetUserWalletRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetUserWalletRequest, GetUserWalletQuery, GetUserWalletResponse>(request, context);
    }
    public override async Task<GetAllUserWalletByFilterResponse> GetAllUserWalletByFilter(GetAllUserWalletByFilterRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetAllUserWalletByFilterRequest, GetAllUserWalletByFilterQuery, GetAllUserWalletByFilterResponse>(request, context);
    }
    
    // ============= Customer-specific Methods =============
    
    public override async Task<GetCustomerWalletResponse> GetCustomerWallet(Google.Protobuf.WellKnownTypes.Empty request, ServerCallContext context)
    {
        // Mock response for customer wallet
        return new GetCustomerWalletResponse
        {
            Balance = 150000,
            NetworkBalance = 75000,
            DiscountBalance = 25000
        };
    }
    
    public override async Task<GetCustomerWalletChangeLogResponse> GetCustomerWalletChangeLog(GetCustomerWalletChangeLogRequest request, ServerCallContext context)
    {
        // Mock response for wallet change log
        return new GetCustomerWalletChangeLogResponse
        {
            MetaData = new CMSMicroservice.Protobuf.Protos.MetaData
            {
                CurrentPage = 1,
                TotalPage = 1,
                PageSize = 10,
                TotalCount = 3,
                HasPrevious = false,
                HasNext = false
            },
            Models =
            {
                new CustomerWalletChangeLogModel
                {
                    CurrentBalance = 150000,
                    ChangeValue = 50000,
                    CurrentNetworkBalance = 75000,
                    ChangeNerworkValue = 25000,
                    IsIncrease = true,
                    RefrenceId = 123,
                    CreatedAt = Google.Protobuf.WellKnownTypes.Timestamp.FromDateTime(DateTime.UtcNow.AddDays(-1))
                },
                new CustomerWalletChangeLogModel
                {
                    CurrentBalance = 100000,
                    ChangeValue = -20000,
                    CurrentNetworkBalance = 50000,
                    ChangeNerworkValue = -10000,
                    IsIncrease = false,
                    RefrenceId = 124,
                    CreatedAt = Google.Protobuf.WellKnownTypes.Timestamp.FromDateTime(DateTime.UtcNow.AddDays(-2))
                }
            }
        };
    }
    
    public override async Task<Google.Protobuf.WellKnownTypes.Empty> CustomerWithdrawBalance(CustomerWithdrawBalanceRequest request, ServerCallContext context)
    {
        // Mock implementation - would handle withdrawal
        return new Google.Protobuf.WellKnownTypes.Empty();
    }
    
    public override async Task<GetCustomerWithdrawalsResponse> GetCustomerWithdrawals(GetCustomerWithdrawalsRequest request, ServerCallContext context)
    {
        // Mock response for customer withdrawals
        return new GetCustomerWithdrawalsResponse
        {
            MetaData = new CMSMicroservice.Protobuf.Protos.MetaData
            {
                CurrentPage = 1,
                TotalPage = 1,
                PageSize = 10,
                TotalCount = 1,
                HasPrevious = false,
                HasNext = false
            },
            Models =
            {
                new CustomerWithdrawalModel
                {
                    Id = 1,
                    WeekDefinitionId = 1,
                    WeekDisplayName = "هفته 1 - دی 1403",
                    TotalAmount = 50000,
                    Status = 1, // 0: Pending, 1: Approved, 2: Rejected
                    WithdrawalMethod = 0, // 0: Cash, 1: Diamond
                    IbanNumber = "IR123456789",
                    Created = Google.Protobuf.WellKnownTypes.Timestamp.FromDateTime(DateTime.UtcNow.AddDays(-3))
                }
            }
        };
    }
    
    public override async Task<GetCustomerWithdrawalSettingsResponse> GetCustomerWithdrawalSettings(Google.Protobuf.WellKnownTypes.Empty request, ServerCallContext context)
    {
        // Mock response for withdrawal settings
        return new GetCustomerWithdrawalSettingsResponse
        {
            MinWithdrawalAmount = 50000 // Minimum 50,000 for withdrawal
        };
    }
}
