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.dto.request.CourseCreateRequest;
import com.reading.platform.dto.request.CourseUpdateRequest;
import com.reading.platform.entity.Course;
import com.reading.platform.service.CourseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "Admin - Course", description = "System Course Management APIs for Admin")
@RestController
@RequestMapping("/api/admin/courses")
@RequiredArgsConstructor
@RequireRole(UserRole.ADMIN)
public class AdminCourseController {

    private final CourseService courseService;

    @Operation(summary = "Create system course")
    @PostMapping
    public Result<Course> createCourse(@Valid @RequestBody CourseCreateRequest request) {
        // System courses have null tenantId
        Course course = courseService.createCourse(null, request);
        course.setIsSystem(1);
        return Result.success(course);
    }

    @Operation(summary = "Update course")
    @PutMapping("/{id}")
    public Result<Course> updateCourse(@PathVariable Long id, @RequestBody CourseUpdateRequest request) {
        return Result.success(courseService.updateCourse(id, request));
    }

    @Operation(summary = "Get course by ID")
    @GetMapping("/{id}")
    public Result<Course> getCourse(@PathVariable Long id) {
        return Result.success(courseService.getCourseById(id));
    }

    @Operation(summary = "Get system course page")
    @GetMapping
    public Result<PageResult<Course>> getCoursePage(
            @RequestParam(required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String category) {
        Page<Course> page = courseService.getSystemCoursePage(pageNum, pageSize, keyword, category);
        return Result.success(PageResult.of(page));
    }

    @Operation(summary = "Delete course")
    @DeleteMapping("/{id}")
    public Result<Void> deleteCourse(@PathVariable Long id) {
        courseService.deleteCourse(id);
        return Result.success();
    }

    @Operation(summary = "Publish course")
    @PostMapping("/{id}/publish")
    public Result<Void> publishCourse(@PathVariable Long id) {
        courseService.publishCourse(id);
        return Result.success();
    }

    @Operation(summary = "Archive course")
    @PostMapping("/{id}/archive")
    public Result<Void> archiveCourse(@PathVariable Long id) {
        courseService.archiveCourse(id);
        return Result.success();
    }

}
