using SharedKernel.Extensions;
using Domain.Entities.Wallets.ValueObject;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Admin.Pages.Member.Wallet.Transactions;

public class ViewModel(IMediator mediator) : PageModel
{
    public TransactionDetail Detail { get; set; } = new();

    public sealed class TransactionDetail
    {
        public int ID { get; set; }
        public Guid WalletKey { get; set; }
        public int WalletID { get; set; }
        public int MemberID { get; set; }
        public string MemberEmail { get; set; } = default!;
        public string? MemberName { get; set; }
        public string WalletBalance { get; set; } = "0";
        public string WalletDonationBalance { get; set; } = "0";
        public string TxType { get; set; } = default!;
        public string BalanceType { get; set; } = default!;
        public string Amount { get; set; } = "0";
        public string BalanceAfter { get; set; } = "0";
        public string? Reason { get; set; }
        public string? RefID { get; set; }
        public string? UserID { get; set; }
        public string? Memo { get; set; }
        public string CreatedAt { get; set; } = default!;
    }

    public async Task<IActionResult> OnGetAsync(int id, CancellationToken ct)
    {
        try
        {
            var result = await mediator.Send(new GetWalletTransaction.Query(id), ct);

            Detail = new TransactionDetail
            {
                ID = result.ID,
                WalletKey = result.WalletKey,
                WalletID = result.WalletID,
                MemberID = result.MemberID,
                MemberEmail = result.MemberEmail,
                MemberName = result.MemberName,
                WalletBalance = result.WalletBalance.ToString("N0"),
                WalletDonationBalance = result.WalletDonationBalance.ToString("N0"),
                TxType = GetTxTypeName(result.TxType),
                BalanceType = GetBalanceTypeName(result.BalanceType),
                Amount = result.Amount.ToString("N0"),
                BalanceAfter = result.BalanceAfter.ToString("N0"),
                Reason = result.Reason,
                RefID = result.RefID,
                UserID = result.UserID,
                Memo = result.Memo,
                CreatedAt = result.CreatedAt.GetDateAt()
            };
        }
        catch (Exception e)
        {
            TempData["ErrorMessages"] = e.Message;
            return RedirectToPage("List");
        }

        return Page();
    }

    private static string GetTxTypeName(WalletTransactionType type) => type switch
    {
        WalletTransactionType.Charge => "충전",
        WalletTransactionType.DonationIn => "후원 받음",
        WalletTransactionType.DonationOut => "후원 보냄",
        WalletTransactionType.RewardEarned => "보상 적립",
        WalletTransactionType.Spend => "사용",
        WalletTransactionType.Refund => "환불",
        WalletTransactionType.Lock => "잠금",
        WalletTransactionType.Unlock => "잠금 해제",
        WalletTransactionType.Adjusted => "조정",
        _ => type.ToString()
    };

    private static string GetBalanceTypeName(WalletBalanceType type) => type switch
    {
        WalletBalanceType.PgCharged => "PG 충전",
        WalletBalanceType.Deposit => "직접 입금",
        WalletBalanceType.Donation => "후원",
        WalletBalanceType.Reward => "보상",
        WalletBalanceType.Airdrop => "에어드랍",
        WalletBalanceType.Locked => "잠금",
        WalletBalanceType.Adjusted => "조정",
        _ => type.ToString()
    };
}