// EN: POS commands for PIN auth and device registration.
// VI: Commands POS cho xác thực PIN và đăng ký thiết bị.

using MediatR;
using MerchantService.Domain.AggregatesModel.MerchantStaffAggregate;
using MerchantService.Domain.Exceptions;

namespace MerchantService.API.Application.Commands.Pos;

#region PIN Auth Command

/// <summary>
/// EN: Command to authenticate staff using PIN for POS.
/// VI: Command để xác thực nhân viên sử dụng PIN cho POS.
/// </summary>
public record PinAuthCommand : IRequest<PinAuthResult>
{
    public Guid StaffId { get; init; }
    public string PinCode { get; init; } = null!;
    public string? DeviceId { get; init; }
}

public record PinAuthResult(
    bool IsAuthenticated,
    Guid? StaffId,
    string? StaffEmail,
    string? Role,
    Guid? ShopId,
    string? ErrorMessage);

/// <summary>
/// EN: Handler for PinAuthCommand.
/// VI: Handler cho PinAuthCommand.
/// </summary>
public class PinAuthCommandHandler : IRequestHandler<PinAuthCommand, PinAuthResult>
{
    private readonly IMerchantStaffRepository _staffRepository;
    private readonly ILogger<PinAuthCommandHandler> _logger;

    public PinAuthCommandHandler(
        IMerchantStaffRepository staffRepository,
        ILogger<PinAuthCommandHandler> logger)
    {
        _staffRepository = staffRepository;
        _logger = logger;
    }

    public async Task<PinAuthResult> Handle(PinAuthCommand request, CancellationToken cancellationToken)
    {
        var staff = await _staffRepository.GetByIdAsync(request.StaffId, cancellationToken);
        
        if (staff == null)
        {
            return new PinAuthResult(false, null, null, null, null, "Staff not found");
        }

        if (staff.Status != StaffStatus.Active)
        {
            return new PinAuthResult(false, null, null, null, null, "Staff is not active");
        }

        if (!staff.VerifyPinCode(request.PinCode))
        {
            _logger.LogWarning("PIN verification failed for staff {StaffId}", request.StaffId);
            return new PinAuthResult(false, null, null, null, null, "Invalid PIN");
        }

        // EN: Get first shop assignment if available
        // VI: Lấy shop đầu tiên nếu có
        var shopId = staff.ShopAssignments.FirstOrDefault()?.ShopId;

        _logger.LogInformation("POS authentication successful for staff {StaffId}", staff.Id);

        return new PinAuthResult(
            true,
            staff.Id,
            staff.Email,
            staff.Role.Name,
            shopId,
            null);
    }
}

#endregion

#region Register Device Command

/// <summary>
/// EN: Command to register a POS device.
/// VI: Command để đăng ký thiết bị POS.
/// </summary>
public record RegisterDeviceCommand : IRequest<RegisterDeviceResult>
{
    /// <summary>
    /// EN: User ID extracted from JWT claims by the controller.
    /// VI: User ID được lấy từ JWT claims bởi controller.
    /// </summary>
    public Guid UserId { get; init; }
    public string DeviceId { get; init; } = null!;
    public string DeviceName { get; init; } = null!;
    public string Platform { get; init; } = null!;
    public string? PushToken { get; init; }
}

public record RegisterDeviceResult(Guid StaffId, string DeviceId, bool IsRegistered);

/// <summary>
/// EN: Handler for RegisterDeviceCommand.
/// VI: Handler cho RegisterDeviceCommand.
/// </summary>
public class RegisterDeviceCommandHandler : IRequestHandler<RegisterDeviceCommand, RegisterDeviceResult>
{
    private readonly IMerchantStaffRepository _staffRepository;
    private readonly ILogger<RegisterDeviceCommandHandler> _logger;

    public RegisterDeviceCommandHandler(
        IMerchantStaffRepository staffRepository,
        ILogger<RegisterDeviceCommandHandler> logger)
    {
        _staffRepository = staffRepository;
        _logger = logger;
    }

    public async Task<RegisterDeviceResult> Handle(RegisterDeviceCommand request, CancellationToken cancellationToken)
    {
        // EN: Use user ID passed from the controller
        // VI: Dùng user ID được truyền từ controller
        var staff = await _staffRepository.GetByUserIdAsync(request.UserId, cancellationToken)
            ?? throw new DomainException("Staff not found");

        staff.RegisterDevice(request.DeviceId, request.DeviceName, request.PushToken, request.Platform);

        _staffRepository.Update(staff);
        await _staffRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);

        _logger.LogInformation("Device registered: {DeviceId} for staff {StaffId}", request.DeviceId, staff.Id);

        return new RegisterDeviceResult(staff.Id, request.DeviceId, true);
    }
}

#endregion
