package com.lesingle.creation.controller;

import com.lesingle.creation.common.core.Result;
import com.lesingle.creation.common.security.UserPrincipal;
import com.lesingle.creation.dto.role.CreateRoleDTO;
import com.lesingle.creation.dto.role.UpdateRoleDTO;
import com.lesingle.creation.service.RoleService;
import com.lesingle.creation.vo.role.RoleDetailVO;
import com.lesingle.creation.vo.role.RoleListVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色管理控制器
 */
@Tag(name = "角色管理")
@RestController
@RequestMapping("/api/roles")
@RequiredArgsConstructor
public class RoleController {

    private final RoleService roleService;

    @PostMapping
    @Operation(summary = "创建角色")
    @PreAuthorize("hasAuthority('role:create')")
    public Result<RoleDetailVO> create(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @RequestBody @Validated CreateRoleDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        Long operatorId = userPrincipal.getUserId();
        RoleDetailVO result = roleService.create(dto, tenantId, operatorId);
        return Result.success(result);
    }

    @GetMapping
    @Operation(summary = "角色列表")
    @PreAuthorize("hasAuthority('role:read')")
    public Result<com.baomidou.mybatisplus.extension.plugins.pagination.Page<RoleListVO>> pageList(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize) {
        Long tenantId = userPrincipal.getTenantId();
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<RoleListVO> result =
                roleService.pageList(tenantId, page, pageSize);
        return Result.success(result);
    }

    @GetMapping("/all")
    @Operation(summary = "所有角色列表")
    @PreAuthorize("hasAuthority('role:read')")
    public Result<List<RoleListVO>> list(
            @AuthenticationPrincipal UserPrincipal userPrincipal) {
        Long tenantId = userPrincipal.getTenantId();
        List<RoleListVO> result = roleService.list(tenantId);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "角色详情")
    @PreAuthorize("hasAuthority('role:read')")
    public Result<RoleDetailVO> detail(@PathVariable Long id) {
        RoleDetailVO result = roleService.detail(id);
        return Result.success(result);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新角色")
    @PreAuthorize("hasAuthority('role:update')")
    public Result<RoleDetailVO> update(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id,
            @RequestBody @Validated UpdateRoleDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        RoleDetailVO result = roleService.update(id, dto, tenantId);
        return Result.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除角色")
    @PreAuthorize("hasAuthority('role:delete')")
    public Result<Void> delete(
            @PathVariable Long id) {
        roleService.delete(id);
        return Result.success();
    }
}
