using System.Security.Claims;
using HoneyBox.Admin.Filters;
using HoneyBox.Admin.Models.Common;
using HoneyBox.Admin.Models.Menu;
using HoneyBox.Admin.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace HoneyBox.Admin.Controllers;

/// <summary>
/// 菜单管理控制器
/// </summary>
[ApiController]
[Route("api/admin/menus")]
[Authorize]
public class MenuController : ControllerBase
{
    private readonly IMenuService _menuService;
    private readonly ILogger<MenuController> _logger;

    public MenuController(IMenuService menuService, ILogger<MenuController> logger)
    {
        _menuService = menuService;
        _logger = logger;
    }

    /// <summary>
    /// 获取菜单树
    /// </summary>
    /// <returns>菜单树列表</returns>
    [HttpGet]
    public async Task<ApiResponse<List<MenuTreeDto>>> GetMenuTree()
    {
        var result = await _menuService.GetMenuTreeAsync();
        return ApiResponse<List<MenuTreeDto>>.Success(result);
    }

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

    /// <summary>
    /// 创建菜单
    /// </summary>
    /// <param name="request">创建请求</param>
    /// <returns>新菜单ID</returns>
    [HttpPost]
    [OperationLog("菜单管理", "创建菜单")]
    public async Task<ApiResponse<long>> Create([FromBody] CreateMenuRequest request)
    {
        var id = await _menuService.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] UpdateMenuRequest request)
    {
        await _menuService.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 _menuService.DeleteAsync(id);
        return ApiResponse.Success("删除成功");
    }

    /// <summary>
    /// 获取当前用户菜单
    /// </summary>
    /// <returns>用户菜单树列表</returns>
    [HttpGet("user")]
    public async Task<ApiResponse<List<MenuTreeDto>>> GetUserMenus()
    {
        var userId = GetCurrentUserId();
        var result = await _menuService.GetUserMenusAsync(userId);
        return ApiResponse<List<MenuTreeDto>>.Success(result);
    }

    /// <summary>
    /// 获取当前用户ID
    /// </summary>
    private long GetCurrentUserId()
    {
        var userIdClaim = User.FindFirst(ClaimTypes.NameIdentifier);
        if (userIdClaim == null || !long.TryParse(userIdClaim.Value, out var userId))
        {
            throw new AdminException(AdminErrorCodes.TokenInvalid, "无效的用户身份");
        }
        return userId;
    }
}
