using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MiningService.API.Application.Commands;
using MiningService.API.Application.Queries;

namespace MiningService.API.Controllers;

/// <summary>
/// EN: Mining operations controller.
/// VI: Controller cho các thao tác đào.
/// </summary>
[ApiController]
[Route("api/v1/[controller]")]
[Authorize]
public class MiningController : ControllerBase
{
    private readonly IMediator _mediator;

    public MiningController(IMediator mediator)
    {
        _mediator = mediator;
    }

    /// <summary>
    /// EN: Get current miner status.
    /// VI: Lấy trạng thái thợ đào hiện tại.
    /// </summary>
    [HttpGet("me")]
    [ProducesResponseType(typeof(MinerStatusDto), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> GetMinerStatus([FromQuery] Guid userId, CancellationToken cancellationToken)
    {
        var result = await _mediator.Send(new GetMinerStatusQuery(userId), cancellationToken);
        if (result == null)
            return NotFound();
        return Ok(result);
    }

    /// <summary>
    /// EN: Start a new mining session.
    /// VI: Bắt đầu phiên đào mới.
    /// </summary>
    [HttpPost("start")]
    [ProducesResponseType(typeof(StartMiningResult), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    public async Task<IActionResult> StartMining([FromBody] StartMiningRequest request, CancellationToken cancellationToken)
    {
        var result = await _mediator.Send(new StartMiningCommand(request.UserId), cancellationToken);
        return Ok(result);
    }

    /// <summary>
    /// EN: Claim mining reward from completed session.
    /// VI: Nhận thưởng đào từ phiên hoàn thành.
    /// </summary>
    [HttpPost("claim")]
    [ProducesResponseType(typeof(ClaimMiningRewardResult), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    public async Task<IActionResult> ClaimReward([FromBody] ClaimMiningRequest request, CancellationToken cancellationToken)
    {
        var result = await _mediator.Send(new ClaimMiningRewardCommand(request.UserId), cancellationToken);
        return Ok(result);
    }

    /// <summary>
    /// EN: Get mining history.
    /// VI: Lấy lịch sử đào.
    /// </summary>
    [HttpGet("history")]
    [ProducesResponseType(typeof(MiningHistoryDto), StatusCodes.Status200OK)]
    public async Task<IActionResult> GetHistory([FromQuery] Guid userId, [FromQuery] int page = 1, [FromQuery] int pageSize = 20, CancellationToken cancellationToken = default)
    {
        var result = await _mediator.Send(new GetMiningHistoryQuery(userId, page, pageSize), cancellationToken);
        return Ok(result);
    }

    /// <summary>
    /// EN: Get current mining rate breakdown.
    /// VI: Lấy chi tiết tỷ lệ đào hiện tại.
    /// </summary>
    [HttpGet("rate")]
    [ProducesResponseType(typeof(MiningRateDto), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> GetRate([FromQuery] Guid userId, CancellationToken cancellationToken)
    {
        var result = await _mediator.Send(new GetMiningRateQuery(userId), cancellationToken);
        if (result == null) return NotFound();
        return Ok(result);
    }

    /// <summary>
    /// EN: Get top miners leaderboard.
    /// VI: Lấy bảng xếp hạng thợ đào.
    /// </summary>
    [HttpGet("leaderboard")]
    [AllowAnonymous]
    [ProducesResponseType(typeof(LeaderboardDto), StatusCodes.Status200OK)]
    public async Task<IActionResult> GetLeaderboard([FromQuery] int limit = 100, CancellationToken cancellationToken = default)
    {
        var result = await _mediator.Send(new GetLeaderboardQuery(limit), cancellationToken);
        return Ok(result);
    }
}

public record StartMiningRequest(Guid UserId);
public record ClaimMiningRequest(Guid UserId);

