package com.reading.platform.controller.teacher;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reading.platform.common.response.PageResult;
import com.reading.platform.common.response.Result;
import com.reading.platform.common.security.SecurityUtils;
import com.reading.platform.dto.request.LessonCreateRequest;
import com.reading.platform.dto.request.LessonUpdateRequest;
import com.reading.platform.entity.Lesson;
import com.reading.platform.service.LessonService;
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.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Tag(name = "Teacher - Lesson", description = "Lesson APIs for Teacher")
@RestController
@RequestMapping("/api/teacher/lessons")
@RequiredArgsConstructor
public class TeacherLessonController {

    private final LessonService lessonService;

    @Operation(summary = "Create lesson")
    @PostMapping
    public Result<Lesson> createLesson(@Valid @RequestBody LessonCreateRequest request) {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        return Result.success(lessonService.createLesson(tenantId, request));
    }

    @Operation(summary = "Update lesson")
    @PutMapping("/{id}")
    public Result<Lesson> updateLesson(@PathVariable Long id, @RequestBody LessonUpdateRequest request) {
        return Result.success(lessonService.updateLesson(id, request));
    }

    @Operation(summary = "Get lesson by ID")
    @GetMapping("/{id}")
    public Result<Lesson> getLesson(@PathVariable Long id) {
        return Result.success(lessonService.getLessonById(id));
    }

    @Operation(summary = "Get my lessons")
    @GetMapping
    public Result<PageResult<Lesson>> getMyLessons(
            @RequestParam(required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        Long teacherId = SecurityUtils.getCurrentUserId();
        Page<Lesson> page = lessonService.getTeacherLessons(teacherId, pageNum, pageSize, status, startDate, endDate);
        return Result.success(PageResult.of(page));
    }

    @Operation(summary = "Start lesson")
    @PostMapping("/{id}/start")
    public Result<Void> startLesson(@PathVariable Long id) {
        lessonService.startLesson(id);
        return Result.success();
    }

    @Operation(summary = "Complete lesson")
    @PostMapping("/{id}/complete")
    public Result<Void> completeLesson(@PathVariable Long id) {
        lessonService.completeLesson(id);
        return Result.success();
    }

    @Operation(summary = "Cancel lesson")
    @PostMapping("/{id}/cancel")
    public Result<Void> cancelLesson(@PathVariable Long id) {
        lessonService.cancelLesson(id);
        return Result.success();
    }

    @Operation(summary = "Get today's lessons")
    @GetMapping("/today")
    public Result<List<Lesson>> getTodayLessons() {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        return Result.success(lessonService.getTodayLessons(tenantId));
    }

}
