package com.lesingle.edu.controller.school;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lesingle.edu.common.annotation.Log;
import com.lesingle.edu.common.enums.LogModule;
import com.lesingle.edu.common.enums.LogOperationType;
import com.lesingle.edu.common.response.PageResult;
import com.lesingle.edu.common.response.Result;
import com.lesingle.edu.common.security.SecurityUtils;
import com.lesingle.edu.dto.request.TeacherCreateRequest;
import com.lesingle.edu.dto.request.TeacherUpdateRequest;
import com.lesingle.edu.dto.response.TeacherResponse;
import com.lesingle.edu.entity.Teacher;
import com.lesingle.edu.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.*;

import java.util.List;

@Tag(name = "School - Teacher", description = "Teacher Management APIs for School")
@RestController
@RequestMapping("/api/v1/school/teachers")
@RequiredArgsConstructor
public class SchoolTeacherController {

    private final TeacherService teacherService;

    @Operation(summary = "Create teacher")
    @Log(module = LogModule.TEACHER, type = LogOperationType.CREATE, description = "创建新教师")
    @PostMapping
    public Result<TeacherResponse> createTeacher(@Valid @RequestBody TeacherCreateRequest request) {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        Teacher teacher = teacherService.createTeacher(tenantId, request);
        return Result.success(teacherService.toTeacherResponse(teacher));
    }

    @Operation(summary = "Update teacher")
    @Log(module = LogModule.TEACHER, type = LogOperationType.UPDATE, description = "修改教师信息")
    @PutMapping("/{id}")
    public Result<TeacherResponse> updateTeacher(@PathVariable Long id, @RequestBody TeacherUpdateRequest request) {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        Teacher teacher = teacherService.updateTeacherWithTenantCheck(id, tenantId, request);
        return Result.success(teacherService.toTeacherResponse(teacher));
    }

    @Operation(summary = "Get teacher by ID")
    @GetMapping("/{id}")
    public Result<TeacherResponse> getTeacher(@PathVariable Long id) {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        Teacher teacher = teacherService.getTeacherByIdWithTenantCheck(id, tenantId);
        return Result.success(teacherService.toTeacherResponse(teacher));
    }

    @Operation(summary = "Get teacher page")
    @GetMapping
    public Result<PageResult<TeacherResponse>> getTeacherPage(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") 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);
        List<TeacherResponse> voList = teacherService.toTeacherResponseList(page.getRecords());
        return Result.success(PageResult.of(voList, page.getTotal(), page.getCurrent(), page.getSize()));
    }

    @Operation(summary = "Delete teacher")
    @Log(module = LogModule.TEACHER, type = LogOperationType.DELETE, description = "删除教师")
    @DeleteMapping("/{id}")
    public Result<Void> deleteTeacher(@PathVariable Long id) {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        teacherService.deleteTeacherWithTenantCheck(id, tenantId);
        return Result.success();
    }

    @Operation(summary = "Reset teacher password")
    @Log(module = LogModule.TEACHER, type = LogOperationType.UPDATE, description = "重置教师密码")
    @PostMapping("/{id}/reset-password")
    public Result<java.util.Map<String, String>> resetPassword(@PathVariable Long id) {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        String tempPassword = teacherService.resetPasswordAndReturnTemp(id, tenantId);
        return Result.success(java.util.Map.of("tempPassword", tempPassword));
    }

}
