using HoneyBox.Admin.Filters;
using HoneyBox.Admin.Models.Common;
using HoneyBox.Admin.Models.Role;
using HoneyBox.Admin.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace HoneyBox.Admin.Controllers;

/// <summary>
/// 角色管理控制器
/// </summary>
[ApiController]
[Route("api/admin/roles")]
[Authorize]
public class RoleController : ControllerBase
{
    private readonly IRoleService _roleService;
    private readonly ILogger<RoleController> _logger;

    public RoleController(IRoleService roleService, ILogger<RoleController> logger)
    {
        _roleService = roleService;
        _logger = logger;
    }

    /// <summary>
    /// 获取角色分页列表
    /// </summary>
    /// <param name="request">查询请求</param>
    /// <returns>分页结果</returns>
    [HttpGet]
    public async Task<ApiResponse<PagedResult<RoleDto>>> GetList([FromQuery] RoleQueryRequest request)
    {
        var result = await _roleService.GetListAsync(request);
        return ApiResponse<PagedResult<RoleDto>>.Success(result);
    }

    /// <summary>
    /// 获取所有角色（下拉选择用）
    /// </summary>
    /// <returns>角色列表</returns>
    [HttpGet("all")]
    public async Task<ApiResponse<List<RoleDto>>> GetAll()
    {
        var result = await _roleService.GetAllAsync();
        return ApiResponse<List<RoleDto>>.Success(result);
    }

    /// <summary>
    /// 获取角色详情
    /// </summary>
    /// <param name="id">角色ID</param>
    /// <returns>角色详情</returns>
    [HttpGet("{id:long}")]
    public async Task<ApiResponse<RoleDto>> GetById(long id)
    {
        var result = await _roleService.GetByIdAsync(id);
        return ApiResponse<RoleDto>.Success(result);
    }

    /// <summary>
    /// 创建角色
    /// </summary>
    /// <param name="request">创建请求</param>
    /// <returns>新角色ID</returns>
    [HttpPost]
    [OperationLog("角色管理", "创建角色")]
    public async Task<ApiResponse<long>> Create([FromBody] CreateRoleRequest request)
    {
        var id = await _roleService.CreateAsync(request);
        return ApiResponse<long>.Success(id, "创建成功");
    }

    /// <summary>
    /// 更新角色
    /// </summary>
    /// <param name="id">角色ID</param>
    /// <param name="request">更新请求</param>
    [HttpPut("{id:long}")]
    [OperationLog("角色管理", "更新角色")]
    public async Task<ApiResponse> Update(long id, [FromBody] UpdateRoleRequest request)
    {
        await _roleService.UpdateAsync(id, request);
        return ApiResponse.Success("更新成功");
    }

    /// <summary>
    /// 删除角色
    /// </summary>
    /// <param name="id">角色ID</param>
    [HttpDelete("{id:long}")]
    [OperationLog("角色管理", "删除角色")]
    public async Task<ApiResponse> Delete(long id)
    {
        await _roleService.DeleteAsync(id);
        return ApiResponse.Success("删除成功");
    }

    /// <summary>
    /// 获取角色已分配的菜单ID列表
    /// </summary>
    /// <param name="id">角色ID</param>
    /// <returns>菜单ID列表</returns>
    [HttpGet("{id:long}/menus")]
    public async Task<ApiResponse<List<long>>> GetMenus(long id)
    {
        var result = await _roleService.GetMenuIdsAsync(id);
        return ApiResponse<List<long>>.Success(result);
    }

    /// <summary>
    /// 分配菜单给角色
    /// </summary>
    /// <param name="id">角色ID</param>
    /// <param name="request">分配请求</param>
    [HttpPut("{id:long}/menus")]
    [OperationLog("角色管理", "分配菜单")]
    public async Task<ApiResponse> AssignMenus(long id, [FromBody] AssignMenusRequest request)
    {
        await _roleService.AssignMenusAsync(id, request.MenuIds);
        return ApiResponse.Success("分配成功");
    }

    /// <summary>
    /// 获取角色已分配的权限编码列表
    /// </summary>
    /// <param name="id">角色ID</param>
    /// <returns>权限编码列表</returns>
    [HttpGet("{id:long}/permissions")]
    public async Task<ApiResponse<List<string>>> GetPermissions(long id)
    {
        var result = await _roleService.GetPermissionCodesAsync(id);
        return ApiResponse<List<string>>.Success(result);
    }

    /// <summary>
    /// 分配权限给角色
    /// </summary>
    /// <param name="id">角色ID</param>
    /// <param name="request">分配请求</param>
    [HttpPut("{id:long}/permissions")]
    [OperationLog("角色管理", "分配权限")]
    public async Task<ApiResponse> AssignPermissions(long id, [FromBody] AssignPermissionsRequest request)
    {
        await _roleService.AssignPermissionsAsync(id, request.PermissionCodes);
        return ApiResponse.Success("分配成功");
    }
}
