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

namespace SocialService.API.Controllers;

/// <summary>
/// EN: Controller for managing relationships (friends, following).
/// VI: Controller để quản lý quan hệ (bạn bè, theo dõi).
/// </summary>
[ApiController]
[Route("api/v1/[controller]")]
[Authorize]
public class RelationshipsController : ControllerBase
{
    private readonly IMediator _mediator;
    private readonly ILogger<RelationshipsController> _logger;

    public RelationshipsController(IMediator mediator, ILogger<RelationshipsController> logger)
    {
        _mediator = mediator;
        _logger = logger;
    }

    #region Friends

    /// <summary>
    /// EN: Get friends of a user.
    /// VI: Lấy danh sách bạn bè của user.
    /// </summary>
    [HttpGet("users/{userId}/friends")]
    [ProducesResponseType(typeof(GetFriendsResult), StatusCodes.Status200OK)]
    public async Task<IActionResult> GetFriends(Guid userId, [FromQuery] int skip = 0, [FromQuery] int take = 20)
    {
        var query = new GetFriendsQuery(userId, skip, take);
        var result = await _mediator.Send(query);
        return Ok(new { success = true, data = result });
    }

    /// <summary>
    /// EN: Send a friend request.
    /// VI: Gửi yêu cầu kết bạn.
    /// </summary>
    [HttpPost("friend-requests")]
    [ProducesResponseType(typeof(SendFriendRequestResult), StatusCodes.Status201Created)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    public async Task<IActionResult> SendFriendRequest([FromBody] SendFriendRequestRequest request)
    {
        var command = new SendFriendRequestCommand(request.RequesterId, request.AddresseeId);
        var result = await _mediator.Send(command);
        return CreatedAtAction(nameof(GetFriends), new { userId = request.RequesterId }, new { success = true, data = result });
    }

    /// <summary>
    /// EN: Respond to a friend request (accept/reject).
    /// VI: Phản hồi yêu cầu kết bạn (chấp nhận/từ chối).
    /// </summary>
    [HttpPut("friend-requests/{relationshipId}")]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> RespondToFriendRequest(Guid relationshipId, [FromBody] RespondToFriendRequestRequest request)
    {
        var command = new RespondToFriendRequestCommand(relationshipId, request.UserId, request.Accept);
        var result = await _mediator.Send(command);
        return Ok(new { success = true, data = new { accepted = result } });
    }

    /// <summary>
    /// EN: Get mutual friends between two users.
    /// VI: Lấy bạn chung giữa hai users.
    /// </summary>
    [HttpGet("users/{userId1}/mutual-friends/{userId2}")]
    [ProducesResponseType(typeof(GetMutualFriendsResult), StatusCodes.Status200OK)]
    public async Task<IActionResult> GetMutualFriends(Guid userId1, Guid userId2)
    {
        var query = new GetMutualFriendsQuery(userId1, userId2);
        var result = await _mediator.Send(query);
        return Ok(new { success = true, data = result });
    }

    /// <summary>
    /// EN: Get friend suggestions for a user.
    /// VI: Lấy gợi ý kết bạn cho user.
    /// </summary>
    [HttpGet("users/{userId}/suggestions")]
    [ProducesResponseType(typeof(GetFriendSuggestionsResult), StatusCodes.Status200OK)]
    public async Task<IActionResult> GetFriendSuggestions(Guid userId, [FromQuery] int limit = 10)
    {
        var query = new GetFriendSuggestionsQuery(userId, limit);
        var result = await _mediator.Send(query);
        return Ok(new { success = true, data = result });
    }

    #endregion

    #region Following

    /// <summary>
    /// EN: Follow a user.
    /// VI: Theo dõi user.
    /// </summary>
    [HttpPost("follow")]
    [ProducesResponseType(typeof(FollowUserResult), StatusCodes.Status201Created)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    public async Task<IActionResult> FollowUser([FromBody] FollowUserRequest request)
    {
        var command = new FollowUserCommand(request.FollowerId, request.FolloweeId);
        var result = await _mediator.Send(command);
        return CreatedAtAction(nameof(GetFriends), new { userId = request.FollowerId }, new { success = true, data = result });
    }

    /// <summary>
    /// EN: Unfollow a user.
    /// VI: Bỏ theo dõi user.
    /// </summary>
    [HttpDelete("follow")]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    public async Task<IActionResult> UnfollowUser([FromBody] UnfollowUserRequest request)
    {
        var command = new UnfollowUserCommand(request.FollowerId, request.FolloweeId);
        var result = await _mediator.Send(command);
        return Ok(new { success = true, data = new { unfollowed = result } });
    }

    #endregion
}

#region Request DTOs

public record SendFriendRequestRequest(Guid RequesterId, Guid AddresseeId);
public record RespondToFriendRequestRequest(Guid UserId, bool Accept);
public record FollowUserRequest(Guid FollowerId, Guid FolloweeId);
public record UnfollowUserRequest(Guid FollowerId, Guid FolloweeId);

#endregion
