package com.lesingle.creation.controller;

import com.lesingle.creation.common.core.Result;
import com.lesingle.creation.common.security.UserPrincipal;
import com.lesingle.creation.dto.permission.CreatePermissionDTO;
import com.lesingle.creation.dto.permission.UpdatePermissionDTO;
import com.lesingle.creation.service.PermissionService;
import com.lesingle.creation.vo.permission.PermissionDetailVO;
import com.lesingle.creation.vo.permission.PermissionListVO;
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/permissions")
@RequiredArgsConstructor
public class PermissionController {

    private final PermissionService permissionService;

    @PostMapping
    @Operation(summary = "创建权限")
    @PreAuthorize("hasAuthority('super_admin')")
    public Result<PermissionDetailVO> create(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @RequestBody @Validated CreatePermissionDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        Long operatorId = userPrincipal.getUserId();
        PermissionDetailVO result = permissionService.create(dto, tenantId, operatorId);
        return Result.success(result);
    }

    @GetMapping
    @Operation(summary = "权限列表")
    @PreAuthorize("hasAuthority('permission:read')")
    public Result<com.baomidou.mybatisplus.extension.plugins.pagination.Page<PermissionListVO>> 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<PermissionListVO> result =
                permissionService.pageList(tenantId, page, pageSize);
        return Result.success(result);
    }

    @GetMapping("/all")
    @Operation(summary = "所有权限列表")
    @PreAuthorize("hasAuthority('permission:read')")
    public Result<List<PermissionListVO>> list(
            @AuthenticationPrincipal UserPrincipal userPrincipal) {
        Long tenantId = userPrincipal.getTenantId();
        List<PermissionListVO> result = permissionService.list(tenantId);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "权限详情")
    @PreAuthorize("hasAuthority('permission:read')")
    public Result<PermissionDetailVO> detail(@PathVariable Long id) {
        PermissionDetailVO result = permissionService.detail(id);
        return Result.success(result);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新权限")
    @PreAuthorize("hasAuthority('super_admin')")
    public Result<PermissionDetailVO> update(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id,
            @RequestBody @Validated UpdatePermissionDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        PermissionDetailVO result = permissionService.update(id, dto, tenantId);
        return Result.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除权限")
    @PreAuthorize("hasAuthority('super_admin')")
    public Result<Void> delete(
            @PathVariable Long id) {
        permissionService.delete(id);
        return Result.success();
    }
}
