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

namespace HoneyBox.Admin.Controllers;

/// <summary>
/// 权限管理控制器
/// </summary>
[ApiController]
[Route("api/admin/permissions")]
[Authorize]
public class PermissionController : ControllerBase
{
    private readonly IPermissionService _permissionService;
    private readonly ILogger<PermissionController> _logger;

    public PermissionController(IPermissionService permissionService, ILogger<PermissionController> logger)
    {
        _permissionService = permissionService;
        _logger = logger;
    }

    /// <summary>
    /// 获取所有权限列表
    /// </summary>
    /// <returns>权限列表</returns>
    [HttpGet]
    public async Task<ApiResponse<List<PermissionDto>>> GetAll()
    {
        var result = await _permissionService.GetAllPermissionsAsync();
        return ApiResponse<List<PermissionDto>>.Success(result);
    }

    /// <summary>
    /// 按模块分组获取权限列表
    /// </summary>
    /// <returns>按模块分组的权限列表</returns>
    [HttpGet("by-module")]
    public async Task<ApiResponse<Dictionary<string, List<PermissionDto>>>> GetByModule()
    {
        var result = await _permissionService.GetPermissionsByModuleAsync();
        return ApiResponse<Dictionary<string, List<PermissionDto>>>.Success(result);
    }

    /// <summary>
    /// 获取权限详情
    /// </summary>
    /// <param name="id">权限ID</param>
    /// <returns>权限详情</returns>
    [HttpGet("{id}")]
    [AdminPermission("permission:detail")]
    [OperationLog("权限管理", "查看权限详情")]
    public async Task<ApiResponse<PermissionDto>> GetById(long id)
    {
        var result = await _permissionService.GetByIdAsync(id);
        if (result == null)
        {
            return ApiResponse<PermissionDto>.Error(404, "权限不存在");
        }
        return ApiResponse<PermissionDto>.Success(result);
    }

    /// <summary>
    /// 创建权限
    /// </summary>
    /// <param name="request">创建请求</param>
    /// <returns>创建的权限</returns>
    [HttpPost]
    [AdminPermission("permission:create")]
    [OperationLog("权限管理", "创建权限")]
    public async Task<ApiResponse<PermissionDto>> Create([FromBody] CreatePermissionRequest request)
    {
        // 检查编码是否已存在
        if (await _permissionService.CodeExistsAsync(request.Code))
        {
            return ApiResponse<PermissionDto>.Error(400, "权限编码已存在");
        }

        var result = await _permissionService.CreateAsync(request);
        return ApiResponse<PermissionDto>.Success(result);
    }

    /// <summary>
    /// 更新权限
    /// </summary>
    /// <param name="id">权限ID</param>
    /// <param name="request">更新请求</param>
    /// <returns>更新后的权限</returns>
    [HttpPut("{id}")]
    [AdminPermission("permission:update")]
    [OperationLog("权限管理", "更新权限")]
    public async Task<ApiResponse<PermissionDto>> Update(long id, [FromBody] UpdatePermissionRequest request)
    {
        var result = await _permissionService.UpdateAsync(id, request);
        if (result == null)
        {
            return ApiResponse<PermissionDto>.Error(404, "权限不存在");
        }
        return ApiResponse<PermissionDto>.Success(result);
    }

    /// <summary>
    /// 删除权限
    /// </summary>
    /// <param name="id">权限ID</param>
    /// <returns>是否删除成功</returns>
    [HttpDelete("{id}")]
    [AdminPermission("permission:delete")]
    [OperationLog("权限管理", "删除权限")]
    public async Task<ApiResponse<bool>> Delete(long id)
    {
        var result = await _permissionService.DeleteAsync(id);
        if (!result)
        {
            return ApiResponse<bool>.Error(404, "权限不存在");
        }
        return ApiResponse<bool>.Success(true);
    }
}
