package com.reading.platform.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reading.platform.common.annotation.RequireRole;
import com.reading.platform.common.enums.UserRole;
import com.reading.platform.common.response.PageResult;
import com.reading.platform.common.response.Result;
import com.reading.platform.entity.ResourceItem;
import com.reading.platform.entity.ResourceLibrary;
import com.reading.platform.service.ResourceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "管理员 - 资源", description = "资源库管理接口（管理员专用）")
@RestController
@RequestMapping("/api/v1/admin/resources")
@RequiredArgsConstructor
@RequireRole(UserRole.ADMIN)
public class AdminResourceController {

    private final ResourceService resourceService;

    @Operation(summary = "获取所有资源库")
    @GetMapping("/libraries")
    public Result<List<ResourceLibrary>> getLibraries(@RequestParam(required = false) String tenantId) {
        return Result.success(resourceService.getLibraries(tenantId));
    }

    @Operation(summary = "创建资源库")
    @PostMapping("/libraries")
    public Result<ResourceLibrary> createLibrary(@RequestBody ResourceLibrary library) {
        return Result.success(resourceService.createLibrary(library));
    }

    @Operation(summary = "更新资源库")
    @PutMapping("/libraries/{id}")
    public Result<ResourceLibrary> updateLibrary(@PathVariable String id, @RequestBody ResourceLibrary library) {
        return Result.success(resourceService.updateLibrary(id, library));
    }

    @Operation(summary = "删除资源库")
    @DeleteMapping("/libraries/{id}")
    public Result<Void> deleteLibrary(@PathVariable String id) {
        resourceService.deleteLibrary(id);
        return Result.success();
    }

    @Operation(summary = "获取资源项")
    @GetMapping("/items")
    public Result<PageResult<ResourceItem>> getItems(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "20") int pageSize,
            @RequestParam(required = false) String libraryId,
            @RequestParam(required = false) String keyword) {
        Page<ResourceItem> page = resourceService.getItems(pageNum, pageSize, libraryId, keyword);
        return Result.success(PageResult.of(page));
    }

    @Operation(summary = "创建资源项")
    @PostMapping("/items")
    public Result<ResourceItem> createItem(@RequestBody ResourceItem item) {
        return Result.success(resourceService.createItem(item));
    }

    @Operation(summary = "更新资源项")
    @PutMapping("/items/{id}")
    public Result<ResourceItem> updateItem(@PathVariable String id, @RequestBody ResourceItem item) {
        return Result.success(resourceService.updateItem(id, item));
    }

    @Operation(summary = "删除资源项")
    @DeleteMapping("/items/{id}")
    public Result<Void> deleteItem(@PathVariable String id) {
        resourceService.deleteItem(id);
        return Result.success();
    }
}
