package com.reading.platform.controller.admin;

import com.reading.platform.common.annotation.RequireRole;
import com.reading.platform.common.enums.UserRole;
import com.reading.platform.common.response.Result;
import com.reading.platform.entity.ProductBundleCoursePackage;
import com.reading.platform.service.ProductBundleCoursePackageService;
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;
import java.util.Map;

/**
 * 管理员 - 套餐课程包关联管理控制器
 */
@Tag(name = "管理员 - 套餐课程包关联", description = "套餐课程包关联管理接口")
@RestController
@RequestMapping("/api/v1/admin/bundles/{bundleId}/packages")
@RequiredArgsConstructor
@RequireRole(UserRole.ADMIN)
public class AdminBundleCoursePackageController {

    private final ProductBundleCoursePackageService bundleCoursePackageService;

    @Operation(summary = "获取套餐包含的课程包列表")
    @GetMapping
    public Result<List<ProductBundleCoursePackage>> getBundleCoursePackages(@PathVariable String bundleId) {
        return Result.success(bundleCoursePackageService.getBundleCoursePackages(bundleId));
    }

    @Operation(summary = "添加课程包到套餐")
    @PostMapping
    public Result<ProductBundleCoursePackage> addBundleCoursePackage(
            @PathVariable String bundleId,
            @RequestBody Map<String, String> body) {
        String coursePackageId = body.get("coursePackageId");
        String gradeLevel = body.get("gradeLevel");

        ProductBundleCoursePackage relation = new ProductBundleCoursePackage();
        relation.setProductBundleId(bundleId);
        relation.setCoursePackageId(coursePackageId);
        relation.setGradeLevel(gradeLevel);

        return Result.success(bundleCoursePackageService.addBundleCoursePackage(relation));
    }

    @Operation(summary = "从套餐移除课程包")
    @DeleteMapping("/{id}")
    public Result<Void> removeBundleCoursePackage(
            @PathVariable String bundleId,
            @PathVariable String id) {
        bundleCoursePackageService.removeBundleCoursePackage(id);
        return Result.success();
    }

    @Operation(summary = "更新课程包排序")
    @PutMapping("/{id}/sort")
    public Result<Void> updateSortOrder(
            @PathVariable String bundleId,
            @PathVariable String id,
            @RequestBody Map<String, Integer> body) {
        Integer sortOrder = body.get("sortOrder");
        bundleCoursePackageService.updateSortOrder(id, sortOrder);
        return Result.success();
    }

    @Operation(summary = "批量添加课程包到套餐")
    @PostMapping("/batch")
    public Result<Void> batchAddBundleCoursePackages(
            @PathVariable String bundleId,
            @RequestBody List<String> coursePackageIds) {
        bundleCoursePackageService.batchAddBundleCoursePackages(bundleId, coursePackageIds);
        return Result.success();
    }
}
