package com.reading.platform.controller.school;

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.common.security.SecurityUtils;
import com.reading.platform.dto.response.ResourceStatsResponse;
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/school/resources")
@RequiredArgsConstructor
@RequireRole(UserRole.SCHOOL)
public class SchoolResourceController {

    private final ResourceService resourceService;

    // ==================== 资源库管理 ====================

    @Operation(summary = "获取本校资源库列表")
    @GetMapping("/libraries")
    public Result<List<ResourceLibrary>> getLibraries() {
        String tenantId = SecurityUtils.getCurrentTenantId();
        return Result.success(resourceService.getTenantLibraries(tenantId));
    }

    @Operation(summary = "根据 ID 获取资源库")
    @GetMapping("/libraries/{id}")
    public Result<ResourceLibrary> getLibrary(@PathVariable String id) {
        return Result.success(resourceService.getLibraryById(id));
    }

    @Operation(summary = "创建资源库")
    @PostMapping("/libraries")
    public Result<ResourceLibrary> createLibrary(@RequestBody ResourceLibrary library) {
        String tenantId = SecurityUtils.getCurrentTenantId();
        String userId = SecurityUtils.getCurrentUserId();
        library.setTenantId(tenantId);
        library.setCreatedBy(userId);
        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(value = "page", required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String libraryId,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String type) {
        String tenantId = SecurityUtils.getCurrentTenantId();
        Page<ResourceItem> page = resourceService.getItemsByTenant(
                tenantId, pageNum, pageSize, libraryId, keyword, type);
        return Result.success(PageResult.of(page));
    }

    @Operation(summary = "根据 ID 获取资源项")
    @GetMapping("/items/{id}")
    public Result<ResourceItem> getItem(@PathVariable String id) {
        return Result.success(resourceService.getItemById(id));
    }

    @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();
    }

    @Operation(summary = "批量删除资源项")
    @PostMapping("/items/batch-delete")
    public Result<Void> batchDeleteItems(@RequestBody List<String> ids) {
        resourceService.batchDeleteItems(ids);
        return Result.success();
    }

    // ==================== 资源统计 ====================

    @Operation(summary = "获取资源统计")
    @GetMapping("/stats")
    public Result<List<ResourceStatsResponse>> getStats() {
        String tenantId = SecurityUtils.getCurrentTenantId();
        return Result.success(resourceService.getStats(tenantId));
    }
}
