package com.reading.platform.controller.school;

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.TeacherCreateRequest;
import com.reading.platform.dto.request.TeacherUpdateRequest;
import com.reading.platform.entity.Teacher;
import com.reading.platform.service.TeacherService;
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 = "School - Teacher", description = "Teacher Management APIs for School")
@RestController
@RequestMapping("/api/school/teachers")
@RequiredArgsConstructor
public class SchoolTeacherController {

    private final TeacherService teacherService;

    @Operation(summary = "Create teacher")
    @PostMapping
    public Result<Teacher> createTeacher(@Valid @RequestBody TeacherCreateRequest request) {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        return Result.success(teacherService.createTeacher(tenantId, request));
    }

    @Operation(summary = "Update teacher")
    @PutMapping("/{id}")
    public Result<Teacher> updateTeacher(@PathVariable Long id, @RequestBody TeacherUpdateRequest request) {
        return Result.success(teacherService.updateTeacher(id, request));
    }

    @Operation(summary = "Get teacher by ID")
    @GetMapping("/{id}")
    public Result<Teacher> getTeacher(@PathVariable Long id) {
        return Result.success(teacherService.getTeacherById(id));
    }

    @Operation(summary = "Get teacher page")
    @GetMapping
    public Result<PageResult<Teacher>> getTeacherPage(
            @RequestParam(required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String status) {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        Page<Teacher> page = teacherService.getTeacherPage(tenantId, pageNum, pageSize, keyword, status);
        return Result.success(PageResult.of(page));
    }

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

    @Operation(summary = "Reset teacher password")
    @PostMapping("/{id}/reset-password")
    public Result<Void> resetPassword(@PathVariable Long id, @RequestParam String newPassword) {
        teacherService.resetPassword(id, newPassword);
        return Result.success();
    }

}
