using Microsoft.AspNetCore.Mvc;
using QYZH.InteractiveMagazine.IService;
using QYZH.InteractiveMagazine.Models.Common;
using QYZH.InteractiveMagazine.Models.Dto;
using QYZH.InteractiveMagazine.Models.Enum;

namespace QYZH.InteractiveMagazine.WebApi.Controllers;

/// <summary>
/// 后台权限管理
/// </summary>
[ApiController]
[Route("api/[controller]")]
[ApiExplorerSettings(GroupName = nameof(ApiVersionEnum.Platform))]
public class PermissionController(IAdminPermissionService adminPermissionService) : BaseController
{
    /// <summary>
    /// 创建菜单
    /// </summary>
    [HttpPost("menus")]
    public async Task<BaseResponse<AdminMenuOutput>> CreateMenuAsync([FromBody] AdminMenuInput input)
    {
        var result = await adminPermissionService.CreateMenuAsync(input);
        return Success(result);
    }

    /// <summary>
    /// 更新菜单
    /// </summary>
    [HttpPut("menus/{id}")]
    public async Task<BaseResponse<AdminMenuOutput>> UpdateMenuAsync(long id, [FromBody] AdminMenuInput input)
    {
        var result = await adminPermissionService.UpdateMenuAsync(id, input);
        return Success(result);
    }

    /// <summary>
    /// 删除菜单
    /// </summary>
    [HttpDelete("menus/{id}")]
    public async Task<BaseResponse<object>> DeleteMenuAsync(long id)
    {
        await adminPermissionService.DeleteMenuAsync(id);
        return Success(new object());
    }

    /// <summary>
    /// 获取菜单树
    /// </summary>
    [HttpPost("menus/tree")]
    public async Task<BaseResponse<List<AdminMenuOutput>>> GetMenuTreeAsync([FromBody] AdminMenuQueryInput input)
    {
        var result = await adminPermissionService.GetMenuTreeAsync(input);
        return Success(result);
    }

    /// <summary>
    /// 获取当前管理员菜单树
    /// </summary>
    [HttpGet("menus/current")]
    public async Task<BaseResponse<List<AdminMenuOutput>>> GetCurrentMenuTreeAsync()
    {
        var userId = GetCurrentUserId();
        BusinessException.ThrowIf(userId == null, "未获取到用户信息", ResultCode.DENY);

        var result = await adminPermissionService.GetAdminUserMenuTreeAsync(userId!.Value);
        return Success(result);
    }

    /// <summary>
    /// 创建角色
    /// </summary>
    [HttpPost("roles")]
    public async Task<BaseResponse<AdminRoleOutput>> CreateRoleAsync([FromBody] AdminRoleInput input)
    {
        var result = await adminPermissionService.CreateRoleAsync(input);
        return Success(result);
    }

    /// <summary>
    /// 更新角色
    /// </summary>
    [HttpPut("roles/{id}")]
    public async Task<BaseResponse<AdminRoleOutput>> UpdateRoleAsync(long id, [FromBody] AdminRoleInput input)
    {
        var result = await adminPermissionService.UpdateRoleAsync(id, input);
        return Success(result);
    }

    /// <summary>
    /// 删除角色
    /// </summary>
    [HttpDelete("roles/{id}")]
    public async Task<BaseResponse<object>> DeleteRoleAsync(long id)
    {
        await adminPermissionService.DeleteRoleAsync(id);
        return Success(new object());
    }

    /// <summary>
    /// 获取角色详情
    /// </summary>
    [HttpGet("roles/{id}")]
    public async Task<BaseResponse<AdminRoleOutput>> GetRoleByIdAsync(long id)
    {
        var result = await adminPermissionService.GetRoleByIdAsync(id);
        return Success(result);
    }

    /// <summary>
    /// 获取角色分页列表
    /// </summary>
    [HttpPost("roles/list")]
    public async Task<BaseResponse<PageListModel<AdminRoleOutput>>> GetRoleListAsync([FromBody] AdminRoleQueryInput input)
    {
        var result = await adminPermissionService.GetRoleListAsync(input);
        return Success(result);
    }

    /// <summary>
    /// 分配角色菜单
    /// </summary>
    [HttpPut("roles/{roleId}/menus")]
    public async Task<BaseResponse<object>> AssignRoleMenusAsync(long roleId, [FromBody] AssignRoleMenusInput input)
    {
        await adminPermissionService.AssignRoleMenusAsync(roleId, input);
        return Success(new object());
    }

    /// <summary>
    /// 分配管理员角色
    /// </summary>
    [HttpPut("users/{adminUserId}/roles")]
    public async Task<BaseResponse<object>> AssignAdminUserRolesAsync(long adminUserId, [FromBody] AssignAdminUserRolesInput input)
    {
        await adminPermissionService.AssignAdminUserRolesAsync(adminUserId, input);
        return Success(new object());
    }
}
