package com.qqchen.deploy.backend.system.api;

import com.qqchen.deploy.backend.system.entity.Role;
import com.qqchen.deploy.backend.framework.api.Response;
import com.qqchen.deploy.backend.framework.controller.BaseController;
import com.qqchen.deploy.backend.system.model.PermissionDTO;
import com.qqchen.deploy.backend.system.model.RoleDTO;
import com.qqchen.deploy.backend.system.model.query.RoleQuery;
import com.qqchen.deploy.backend.system.model.response.RoleMenusAndPermissionsResponse;
import com.qqchen.deploy.backend.system.service.IRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.CompletableFuture;

@Tag(name = "角色管理")
@RestController
@RequestMapping("/api/v1/role")
public class RoleApiController extends BaseController<Role, RoleDTO, Long, RoleQuery> {

    @Resource
    private IRoleService roleService;

    @Override
    public Response<RoleDTO> create(RoleDTO dto) {
        return super.create(dto);
    }

    @Override
    public Response<RoleDTO> update(Long aLong, RoleDTO dto) {
        return super.update(aLong, dto);
    }

    @Override
    public Response<Void> delete(Long aLong) {
        return super.delete(aLong);
    }

    @Override
    public Response<RoleDTO> findById(Long aLong) {
        return super.findById(aLong);
    }

    @Override
    public Response<List<RoleDTO>> findAll() {
        return super.findAll();
    }

    @Override
    public Response<Page<RoleDTO>> page(RoleQuery query) {
        return super.page(query);
    }

    @Override
    public Response<List<RoleDTO>> findAll(RoleQuery query) {
        return super.findAll(query);
    }

    @Override
    public CompletableFuture<Response<Void>> batchProcess(List<RoleDTO> dtos) {
        return super.batchProcess(dtos);
    }

    @Operation(summary = "分配标签")
    @PostMapping("/{id}/tags")
    public Response<Void> assignTags(@PathVariable Long id, @RequestBody List<Long> tagIds) {
        roleService.assignTags(id, tagIds);
        return Response.success();
    }

    @Operation(summary = "获取角色菜单和权限", description = "返回菜单ID列表和功能权限点ID列表")
    @GetMapping("/{id}/menus-and-permissions")
    public Response<RoleMenusAndPermissionsResponse> getRoleMenusAndPermissions(
        @Parameter(description = "角色ID", required = true) @PathVariable Long id
    ) {
        return Response.success(roleService.getRoleMenusAndPermissions(id));
    }

    @Operation(summary = "分配菜单和权限", description = "同时分配菜单和功能权限点，后端自动补全父菜单")
    @PostMapping("/{id}/menus-and-permissions")
    public Response<Void> assignMenusAndPermissions(
        @Parameter(description = "角色ID", required = true) @PathVariable Long id,
        @Parameter(description = "分配权限请求", required = true) @RequestBody com.qqchen.deploy.backend.system.model.request.AssignPermissionsRequest request
    ) {
        roleService.assignMenusAndPermissions(id, request.getMenuIds(), request.getPermissionIds());
        return Response.success();
    }

    @Override
    protected void exportData(jakarta.servlet.http.HttpServletResponse response, List<RoleDTO> data) {
        // TODO: 实现导出功能
    }
} 