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.*;

import java.util.List;
import java.util.Map;

@Tag(name = "管理员 - 课程", description = "系统课程管理接口（管理员专用）")
@RestController
@RequestMapping("/api/v1/admin/courses")
@RequiredArgsConstructor
@RequireRole(UserRole.ADMIN)
public class AdminCourseController {

    private final CourseService courseService;

    @Operation(summary = "创建系统课程")
    @PostMapping
    public Result<Course> createCourse(@Valid @RequestBody CourseCreateRequest request) {
        // System courses have null tenantId; service sets isSystem=1 when tenantId is null
        return Result.success(courseService.createCourse(null, request));
    }

    @Operation(summary = "更新课程")
    @PutMapping("/{id}")
    public Result<Course> updateCourse(@PathVariable String id, @RequestBody CourseUpdateRequest request) {
        return Result.success(courseService.updateCourse(id, request));
    }

    @Operation(summary = "根据ID获取课程")
    @GetMapping("/{id}")
    public Result<Course> getCourse(@PathVariable String id) {
        return Result.success(courseService.getCourseById(id));
    }

    @Operation(summary = "获取系统课程分页（所有状态）")
    @GetMapping
    public Result<PageResult<Course>> getCoursePage(
            @RequestParam(value = "page", required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) String status) {
        Page<Course> page = courseService.getSystemCoursePage(pageNum, pageSize, keyword, category, status);
        return Result.success(PageResult.of(page));
    }

    @Operation(summary = "获取待审核课程")
    @GetMapping("/review")
    public Result<PageResult<Course>> getReviewCoursePage(
            @RequestParam(value = "page", required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize) {
        Page<Course> page = courseService.getReviewCoursePage(pageNum, pageSize);
        return Result.success(PageResult.of(page));
    }

    @Operation(summary = "删除课程")
    @DeleteMapping("/{id}")
    public Result<Void> deleteCourse(@PathVariable String id) {
        courseService.deleteCourse(id);
        return Result.success();
    }

    @Operation(summary = "提交课程审核")
    @PostMapping("/{id}/submit")
    public Result<Void> submitCourse(@PathVariable String id) {
        courseService.submitCourse(id);
        return Result.success();
    }

    @Operation(summary = "撤销课程审核")
    @PostMapping("/{id}/withdraw")
    public Result<Void> withdrawCourse(@PathVariable String id) {
        courseService.withdrawCourse(id);
        return Result.success();
    }

    @Operation(summary = "审批课程")
    @PostMapping("/{id}/approve")
    public Result<Void> approveCourse(
            @PathVariable String id,
            @RequestParam(required = false) String comment) {
        courseService.approveCourse(id, comment);
        return Result.success();
    }

    @Operation(summary = "驳回课程")
    @PostMapping("/{id}/reject")
    public Result<Void> rejectCourse(
            @PathVariable String id,
            @RequestParam(required = false) String comment) {
        courseService.rejectCourse(id, comment);
        return Result.success();
    }

    @Operation(summary = "发布课程")
    @PostMapping("/{id}/publish")
    public Result<Void> publishCourse(@PathVariable String id) {
        courseService.publishCourse(id);
        return Result.success();
    }

    @Operation(summary = "直接发布课程（跳过审核）")
    @PostMapping("/{id}/direct-publish")
    public Result<Void> directPublishCourse(@PathVariable String id) {
        courseService.publishCourse(id);
        return Result.success();
    }

    @Operation(summary = "取消发布（归档）课程")
    @PostMapping("/{id}/unpublish")
    public Result<Void> unpublishCourse(@PathVariable String id) {
        courseService.archiveCourse(id);
        return Result.success();
    }

    @Operation(summary = "重新发布课程")
    @PostMapping("/{id}/republish")
    public Result<Void> republishCourse(@PathVariable String id) {
        courseService.publishCourse(id);
        return Result.success();
    }

    @Operation(summary = "归档课程")
    @PostMapping("/{id}/archive")
    public Result<Void> archiveCourse(@PathVariable String id) {
        courseService.archiveCourse(id);
        return Result.success();
    }

}
