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.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.web.bind.annotation.*;

import java.util.List;

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

    @Resource
    private IRoleService roleService;

    @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 = "分配角色")
    @PostMapping("/{userId}/assignRoles")
    public Response<Void> assignRoles(
        @Parameter(description = "用户ID", required = true) @PathVariable Long userId,
        @Parameter(description = "角色ID列表", required = true) @RequestBody List<Long> roleIds
    ) {
        roleService.assignRoles(userId, roleIds);
        return Response.success();
    }

    @Operation(summary = "获取角色权限")
    @GetMapping("/{id}/permissions")
    public Response<List<PermissionDTO>> getRolePermissions(
        @Parameter(description = "角色ID", required = true) @PathVariable Long id
    ) {
        return Response.success(roleService.listRolePermissions(id));
    }

    @Operation(summary = "分配权限")
    @PostMapping("/{id}/permissions")
    public Response<Void> assignPermissions(
        @Parameter(description = "角色ID", required = true) @PathVariable Long id,
        @Parameter(description = "权限ID列表", required = true) @RequestBody List<Long> permissionIds
    ) {
        roleService.assignPermissions(id, permissionIds);
        return Response.success();
    }

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