using Application.Abstractions.Messaging;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Admin.Pages.Member.List;

public class ViewModel(IMediator mediator) : PageModel
{
    // 기본 정보
    public int ID { get; set; }
    public string SID { get; set; } = "";
    public string Email { get; set; } = "";
    public string? Name { get; set; }
    public string? FullName { get; set; }
    public string? FirstName { get; set; }
    public string? LastName { get; set; }
    public string? Phone { get; set; }
    public string? Birthday { get; set; }
    public string? Gender { get; set; }
    public string? Summary { get; set; }
    public string? Intro { get; set; }
    public string? GradeName { get; set; }
    public long Following { get; set; }
    public long Followed { get; set; }

    // 상태
    public char IsEmailVerified { get; set; }
    public char IsAuthCertified { get; set; }
    public char IsDenied { get; set; }
    public char IsAdmin { get; set; }
    public char IsWithdraw { get; set; }
    public char IsCreator { get; set; }

    // 상태 일시
    public string? EmailVerifiedAt { get; set; }
    public string? AuthCertifiedAt { get; set; }
    public string? DeniedAt { get; set; }
    public string? DeletedAt { get; set; }

    // 접속/로그 정보
    public string? DeviceInfo { get; set; }
    public string? SignupIP { get; set; }
    public string? LastLoginIp { get; set; }
    public string? IpAddress { get; set; }
    public string? UserAgent { get; set; }
    public string? LastLoginAt { get; set; }
    public string? UpdatedAt { get; set; }
    public string CreatedAt { get; set; } = "";

    // 지갑 정보
    public (long Balance, long CreditBalance) Wallet { get; set; }

    // 채널 정보
    public (string SID, string Name, string? Handle, string YouTubeUrl)? Channel { get; set; }

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

        ID = result.ID;
        SID = result.SID;
        Email = result.Email;
        Name = result.Name;
        FullName = result.FullName;
        FirstName = result.FirstName;
        LastName = result.LastName;
        Phone = result.Phone;
        Birthday = result.Birthday?.ToString("yyyy-MM-dd");
        Gender = result.Gender?.ToString();
        Summary = result.Summary;
        Intro = result.Intro;
        GradeName = result.GradeName;
        Following = result.Following;
        Followed = result.Followed;

        IsEmailVerified = result.IsEmailVerified ? 'Y' : 'N';
        IsAuthCertified = result.IsAuthCertified ? 'Y' : 'N';
        IsDenied = result.IsDenied ? 'Y' : 'N';
        IsAdmin = result.IsAdmin ? 'Y' : 'N';
        IsWithdraw = result.IsWithdraw ? 'Y' : 'N';
        IsCreator = result.IsCreator ? 'Y' : 'N';

        EmailVerifiedAt = result.EmailVerifiedAt?.ToString("yyyy-MM-dd HH:mm:ss");
        AuthCertifiedAt = result.AuthCertifiedAt?.ToString("yyyy-MM-dd HH:mm:ss");
        DeniedAt = result.DeniedAt?.ToString("yyyy-MM-dd HH:mm:ss");
        DeletedAt = result.DeletedAt?.ToString("yyyy-MM-dd HH:mm:ss");

        DeviceInfo = result.DeviceInfo;
        SignupIP = result.SignupIP;
        LastLoginIp = result.LastLoginIp;
        IpAddress = result.IpAddress;
        UserAgent = result.UserAgent;
        LastLoginAt = result.LastLoginAt?.ToString("yyyy-MM-dd HH:mm:ss");
        UpdatedAt = result.UpdatedAt?.ToString("yyyy-MM-dd HH:mm:ss");
        CreatedAt = result.CreatedAt.ToString("yyyy-MM-dd HH:mm:ss");

        Wallet = (result.Wallet.Balance, result.Wallet.CreditBalance);

        if (result.Channel is not null)
        {
            Channel = (result.Channel.SID, result.Channel.Name, result.Channel.Handle, result.Channel.YouTubeUrl);
        }

        ViewData["QueryString"] = Request.QueryString.ToString().TrimStart('?');

        return Page();
    }
}