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

import com.qqchen.deploy.backend.framework.api.Response;
import com.qqchen.deploy.backend.system.entity.Permission;
import com.qqchen.deploy.backend.framework.controller.BaseController;
import com.qqchen.deploy.backend.system.model.PermissionDTO;
import com.qqchen.deploy.backend.system.model.query.PermissionQuery;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "权限管理")
@RestController
@RequestMapping("/api/v1/permission")
public class PermissionApiController extends BaseController<Permission, PermissionDTO, Long, PermissionQuery> {

    @Override
    @PostMapping
    @PreAuthorize("hasRole('ADMIN') or hasAuthority('system:permission:create')")
    public Response<PermissionDTO> create(@Validated @RequestBody PermissionDTO dto) {
        return super.create(dto);
    }

    @Override
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN') or hasAuthority('system:permission:update')")
    public Response<PermissionDTO> update(@PathVariable Long id, @Validated @RequestBody PermissionDTO dto) {
        return super.update(id, dto);
    }

    @Override
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN') or hasAuthority('system:permission:delete')")
    public Response<Void> delete(@PathVariable Long id) {
        return super.delete(id);
    }

    @Override
    @GetMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN') or hasAuthority('system:permission:view') or hasAuthority('system:role:create') or hasAuthority('system:role:update')")
    public Response<PermissionDTO> findById(@PathVariable Long id) {
        return super.findById(id);
    }

    @Override
    @GetMapping
    @PreAuthorize("hasRole('ADMIN') or hasAuthority('system:permission:list') or hasAuthority('system:role:create') or hasAuthority('system:role:update')")
    public Response<List<PermissionDTO>> findAll() {
        return super.findAll();
    }

    @Override
    @GetMapping("/page")
    @PreAuthorize("hasRole('ADMIN') or hasAuthority('system:permission:list') or hasAuthority('system:role:create') or hasAuthority('system:role:update')")
    public Response<Page<PermissionDTO>> page(PermissionQuery query) {
        return super.page(query);
    }

    @Override
    @GetMapping("/list")
    @PreAuthorize("hasRole('ADMIN') or hasAuthority('system:permission:list') or hasAuthority('system:role:create') or hasAuthority('system:role:update')")
    public Response<List<PermissionDTO>> findAll(PermissionQuery query) {
        return super.findAll(query);
    }

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