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 = "学校 - 教师", description = "教师管理接口（学校管理员专用）")
@RestController
@RequestMapping("/api/v1/school/teachers")
@RequiredArgsConstructor
public class SchoolTeacherController {

    private final TeacherService teacherService;

    @Operation(summary = "创建教师")
    @PostMapping
    public Result<Teacher> createTeacher(@Valid @RequestBody TeacherCreateRequest request) {
        String tenantId = SecurityUtils.getCurrentTenantId();
        return Result.success(teacherService.createTeacher(tenantId, request));
    }

    @Operation(summary = "更新教师")
    @PutMapping("/{id}")
    public Result<Teacher> updateTeacher(@PathVariable String id, @RequestBody TeacherUpdateRequest request) {
        return Result.success(teacherService.updateTeacher(id, request));
    }

    @Operation(summary = "根据ID获取教师")
    @GetMapping("/{id}")
    public Result<Teacher> getTeacher(@PathVariable String id) {
        return Result.success(teacherService.getTeacherById(id));
    }

    @Operation(summary = "获取教师分页")
    @GetMapping
    public Result<PageResult<Teacher>> getTeacherPage(
            @RequestParam(value = "page", required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String status) {
        String tenantId = SecurityUtils.getCurrentTenantId();
        Page<Teacher> page = teacherService.getTeacherPage(tenantId, pageNum, pageSize, keyword, status);
        return Result.success(PageResult.of(page));
    }

    @Operation(summary = "删除教师")
    @DeleteMapping("/{id}")
    public Result<Void> deleteTeacher(@PathVariable String id) {
        teacherService.deleteTeacher(id);
        return Result.success();
    }

    @Operation(summary = "重置教师密码")
    @PostMapping("/{id}/reset-password")
    public Result<Void> resetPassword(@PathVariable String id, @RequestParam String newPassword) {
        teacherService.resetPassword(id, newPassword);
        return Result.success();
    }

}
