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: Security circles controller.
/// VI: Controller cho vòng tròn an toàn.
/// </summary>
[ApiController]
[Route("api/v1/[controller]")]
[Authorize]
public class CirclesController : ControllerBase
{
    private readonly IMediator _mediator;

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

    /// <summary>
    /// EN: Get user's circle.
    /// VI: Lấy vòng tròn của người dùng.
    /// </summary>
    [HttpGet("me")]
    [ProducesResponseType(typeof(CircleDto), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> GetMyCircle([FromQuery] Guid userId, CancellationToken ct)
    {
        var result = await _mediator.Send(new GetCircleQuery(userId), ct);
        if (result == null) return NotFound();
        return Ok(result);
    }

    /// <summary>
    /// EN: Create a new security circle.
    /// VI: Tạo vòng tròn an toàn mới.
    /// </summary>
    [HttpPost]
    [ProducesResponseType(typeof(CreateCircleResult), StatusCodes.Status201Created)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    public async Task<IActionResult> CreateCircle([FromBody] CreateCircleRequest request, CancellationToken ct)
    {
        var result = await _mediator.Send(new CreateCircleCommand(request.UserId, request.Name), ct);
        return CreatedAtAction(nameof(GetMyCircle), new { userId = request.UserId }, result);
    }

    /// <summary>
    /// EN: Invite a member to circle.
    /// VI: Mời thành viên vào vòng tròn.
    /// </summary>
    [HttpPost("invite")]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    public async Task<IActionResult> InviteMember([FromBody] InviteMemberRequest request, CancellationToken ct)
    {
        await _mediator.Send(new InviteToCircleCommand(request.UserId, request.TargetMinerId), ct);
        return Ok(new { message = "Member invited successfully" });
    }

    /// <summary>
    /// EN: Accept circle invitation.
    /// VI: Chấp nhận lời mời vòng tròn.
    /// </summary>
    [HttpPost("accept/{inviteId}")]
    [ProducesResponseType(typeof(AcceptCircleInviteResult), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    public async Task<IActionResult> AcceptInvite(Guid inviteId, [FromQuery] Guid userId, CancellationToken ct)
    {
        var result = await _mediator.Send(new AcceptCircleInviteCommand(userId, inviteId), ct);
        return Ok(result);
    }

    /// <summary>
    /// EN: Remove member from circle.
    /// VI: Xóa thành viên khỏi vòng tròn.
    /// </summary>
    [HttpDelete("members/{memberId}")]
    [ProducesResponseType(typeof(RemoveCircleMemberResult), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    public async Task<IActionResult> RemoveMember(Guid memberId, [FromQuery] Guid ownerId, CancellationToken ct)
    {
        var result = await _mediator.Send(new RemoveCircleMemberCommand(ownerId, memberId), ct);
        return Ok(result);
    }

    /// <summary>
    /// EN: Get circle trust score.
    /// VI: Lấy điểm tin cậy vòng tròn.
    /// </summary>
    [HttpGet("trust-score")]
    [ProducesResponseType(typeof(CircleTrustScoreDto), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> GetTrustScore([FromQuery] Guid userId, CancellationToken ct)
    {
        var result = await _mediator.Send(new GetCircleTrustScoreQuery(userId), ct);
        if (result == null) return NotFound();
        return Ok(result);
    }
}

public record CreateCircleRequest(Guid UserId, string Name);
public record InviteMemberRequest(Guid UserId, Guid TargetMinerId);

