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.CoursePackageCourse;
import com.reading.platform.service.CoursePackageCourseService;
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/packages/{packageId}/courses")
@RequiredArgsConstructor
@RequireRole(UserRole.ADMIN)
public class AdminPackageCourseController {

    private final CoursePackageCourseService packageCourseService;

    @Operation(summary = "获取课程包包含的课程列表")
    @GetMapping
    public Result<List<CoursePackageCourse>> getPackageCourses(@PathVariable String packageId) {
        return Result.success(packageCourseService.getPackageCourses(packageId));
    }

    @Operation(summary = "添加课程到课程包")
    @PostMapping
    public Result<CoursePackageCourse> addPackageCourse(
            @PathVariable String packageId,
            @RequestBody Map<String, String> body) {
        String courseId = body.get("courseId");

        CoursePackageCourse relation = new CoursePackageCourse();
        relation.setCoursePackageId(packageId);
        relation.setCourseId(courseId);

        return Result.success(packageCourseService.addPackageCourse(relation));
    }

    @Operation(summary = "从课程包移除课程")
    @DeleteMapping("/{id}")
    public Result<Void> removePackageCourse(
            @PathVariable String packageId,
            @PathVariable String id) {
        packageCourseService.removePackageCourse(id);
        return Result.success();
    }

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

    @Operation(summary = "批量添加课程到课程包")
    @PostMapping("/batch")
    public Result<Void> batchAddPackageCourses(
            @PathVariable String packageId,
            @RequestBody List<String> courseIds) {
        packageCourseService.batchAddPackageCourses(packageId, courseIds);
        return Result.success();
    }
}
