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

namespace SocialService.API.Controllers;

/// <summary>
/// EN: Admin controller for managing social data (relationships, blocks).
/// VI: Admin controller để quản lý dữ liệu xã hội (relationships, blocks).
/// </summary>
[ApiController]
[Route("api/v1/admin/social")]
public class AdminController : ControllerBase
{
    private readonly IMediator _mediator;
    private readonly ILogger<AdminController> _logger;

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

    #region Relationships

    /// <summary>
    /// EN: Get all relationships with pagination and filtering.
    /// VI: Lấy tất cả relationships với phân trang và filter.
    /// </summary>
    [HttpGet("relationships")]
    [ProducesResponseType(typeof(GetAllRelationshipsResult), StatusCodes.Status200OK)]
    public async Task<IActionResult> GetAllRelationships(
        [FromQuery] int skip = 0,
        [FromQuery] int take = 20,
        [FromQuery] string? status = null,
        [FromQuery] string? type = null)
    {
        var query = new GetAllRelationshipsQuery(skip, take, status, type);
        var result = await _mediator.Send(query);
        return Ok(result);
    }

    /// <summary>
    /// EN: Get relationship by ID.
    /// VI: Lấy relationship theo ID.
    /// </summary>
    [HttpGet("relationships/{id:guid}")]
    [ProducesResponseType(typeof(RelationshipDetailDto), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> GetRelationshipById(Guid id)
    {
        var query = new GetRelationshipByIdQuery(id);
        var result = await _mediator.Send(query);
        
        if (result == null)
            return NotFound(new { error = "Relationship not found" });
            
        return Ok(result);
    }

    /// <summary>
    /// EN: Admin delete relationship.
    /// VI: Admin xóa relationship.
    /// </summary>
    [HttpDelete("relationships/{id:guid}")]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> DeleteRelationship(Guid id)
    {
        var command = new AdminDeleteRelationshipCommand(id);
        var result = await _mediator.Send(command);
        
        if (!result)
            return NotFound(new { error = "Relationship not found" });
            
        return Ok(new { success = true, message = "Relationship deleted successfully" });
    }

    #endregion

    #region Blocks

    /// <summary>
    /// EN: Get all blocks with pagination.
    /// VI: Lấy tất cả blocks với phân trang.
    /// </summary>
    [HttpGet("blocks")]
    [ProducesResponseType(typeof(GetAllBlocksResult), StatusCodes.Status200OK)]
    public async Task<IActionResult> GetAllBlocks(
        [FromQuery] int skip = 0,
        [FromQuery] int take = 20)
    {
        var query = new GetAllBlocksQuery(skip, take);
        var result = await _mediator.Send(query);
        return Ok(result);
    }

    /// <summary>
    /// EN: Admin delete block.
    /// VI: Admin xóa block.
    /// </summary>
    [HttpDelete("blocks/{id:guid}")]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> DeleteBlock(Guid id)
    {
        var command = new AdminDeleteBlockCommand(id);
        var result = await _mediator.Send(command);
        
        if (!result)
            return NotFound(new { error = "Block not found" });
            
        return Ok(new { success = true, message = "Block deleted successfully" });
    }

    #endregion

    #region Statistics

    /// <summary>
    /// EN: Get social statistics overview.
    /// VI: Lấy thống kê tổng quan.
    /// </summary>
    [HttpGet("statistics")]
    [ProducesResponseType(typeof(SocialStatisticsDto), StatusCodes.Status200OK)]
    public async Task<IActionResult> GetStatistics()
    {
        var query = new GetSocialStatisticsQuery();
        var result = await _mediator.Send(query);
        return Ok(result);
    }

    #endregion
}
