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.CourseLesson;
import com.reading.platform.service.CourseLessonService;
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/courses/{courseId}/lessons")
@RequiredArgsConstructor
@RequireRole(UserRole.ADMIN)
public class AdminCourseLessonController {

    private final CourseLessonService courseLessonService;

    @Operation(summary = "获取课程的课时列表")
    @GetMapping
    public Result<List<CourseLesson>> getLessons(@PathVariable String courseId) {
        return Result.success(courseLessonService.getLessonsByCourse(courseId));
    }

    @Operation(summary = "根据ID获取课时")
    @GetMapping("/{id}")
    public Result<CourseLesson> getLesson(@PathVariable String courseId, @PathVariable String id) {
        return Result.success(courseLessonService.getLessonById(id));
    }

    @Operation(summary = "创建课程课时")
    @PostMapping
    public Result<CourseLesson> createLesson(@PathVariable String courseId, @RequestBody CourseLesson lesson) {
        lesson.setCourseId(courseId);
        return Result.success(courseLessonService.createLesson(lesson));
    }

    @Operation(summary = "更新课程课时")
    @PutMapping("/{id}")
    public Result<CourseLesson> updateLesson(@PathVariable String courseId, @PathVariable String id, @RequestBody CourseLesson lesson) {
        return Result.success(courseLessonService.updateLesson(id, lesson));
    }

    @Operation(summary = "删除课程课时")
    @DeleteMapping("/{id}")
    public Result<Void> deleteLesson(@PathVariable String courseId, @PathVariable String id) {
        courseLessonService.deleteLesson(id);
        return Result.success();
    }
}
