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

namespace HoneyBox.Admin.Controllers;

/// <summary>
/// 部门管理控制器
/// </summary>
[ApiController]
[Route("api/admin/departments")]
[Authorize]
public class DepartmentController : ControllerBase
{
    private readonly IDepartmentService _departmentService;
    private readonly ILogger<DepartmentController> _logger;

    public DepartmentController(IDepartmentService departmentService, ILogger<DepartmentController> logger)
    {
        _departmentService = departmentService;
        _logger = logger;
    }

    /// <summary>
    /// 获取部门树
    /// </summary>
    /// <returns>部门树列表</returns>
    [HttpGet]
    public async Task<ApiResponse<List<DepartmentTreeDto>>> GetDepartmentTree()
    {
        var result = await _departmentService.GetDepartmentTreeAsync();
        return ApiResponse<List<DepartmentTreeDto>>.Success(result);
    }

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

    /// <summary>
    /// 创建部门
    /// </summary>
    /// <param name="request">创建请求</param>
    /// <returns>新部门ID</returns>
    [HttpPost]
    [OperationLog("部门管理", "创建部门")]
    public async Task<ApiResponse<long>> Create([FromBody] CreateDepartmentRequest request)
    {
        var id = await _departmentService.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] UpdateDepartmentRequest request)
    {
        await _departmentService.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 _departmentService.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 _departmentService.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] AssignDepartmentMenusRequest request)
    {
        await _departmentService.AssignMenusAsync(id, request.MenuIds);
        return ApiResponse.Success("分配成功");
    }

    /// <summary>
    /// 获取部门下的用户列表
    /// </summary>
    /// <param name="id">部门ID</param>
    /// <returns>用户列表</returns>
    [HttpGet("{id:long}/users")]
    public async Task<ApiResponse<List<AdminUserDto>>> GetDepartmentUsers(long id)
    {
        var result = await _departmentService.GetDepartmentUsersAsync(id);
        return ApiResponse<List<AdminUserDto>>.Success(result);
    }
}
