package com.lesingle.creation.controller;

import com.lesingle.creation.common.core.Result;
import com.lesingle.creation.common.security.UserPrincipal;
import com.lesingle.creation.dto.teacher.CreateTeacherDTO;
import com.lesingle.creation.dto.teacher.UpdateTeacherDTO;
import com.lesingle.creation.service.TeacherService;
import com.lesingle.creation.vo.teacher.TeacherVO;
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.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 教师管理控制器
 */
@Tag(name = "教师管理", description = "教师 CRUD 和查询接口")
@RestController
@RequestMapping("/api/teachers")
@RequiredArgsConstructor
public class TeacherController {

    private final TeacherService teacherService;

    @PostMapping
    @Operation(summary = "创建教师")
    @PreAuthorize("hasAuthority('teacher:create')")
    public Result<TeacherVO> create(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @RequestBody @Valid CreateTeacherDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        Long creatorId = userPrincipal.getUserId();
        TeacherVO result = teacherService.create(dto, tenantId, creatorId);
        return Result.success(result);
    }

    @GetMapping
    @Operation(summary = "查询教师列表")
    @PreAuthorize("hasAuthority('teacher:read')")
    public Result<List<TeacherVO>> list(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @RequestParam(required = false) Long departmentId,
            @RequestParam(required = false) String nickname,
            @RequestParam(required = false) String username) {
        Long tenantId = userPrincipal.getTenantId();
        List<TeacherVO> result = teacherService.list(tenantId, departmentId, nickname, username);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取教师详情")
    @PreAuthorize("hasAuthority('teacher:read')")
    public Result<TeacherVO> getDetail(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id) {
        Long tenantId = userPrincipal.getTenantId();
        TeacherVO result = teacherService.getDetail(id, tenantId);
        return Result.success(result);
    }

    @GetMapping("/user/{userId}")
    @Operation(summary = "根据用户 ID 获取教师信息")
    @PreAuthorize("hasAuthority('teacher:read')")
    public Result<TeacherVO> getByUserId(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long userId) {
        Long tenantId = userPrincipal.getTenantId();
        TeacherVO result = teacherService.getByUserId(userId, tenantId);
        return Result.success(result);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新教师")
    @PreAuthorize("hasAuthority('teacher:update')")
    public Result<TeacherVO> update(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id,
            @RequestBody @Valid UpdateTeacherDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        Long modifierId = userPrincipal.getUserId();
        TeacherVO result = teacherService.update(id, dto, tenantId, modifierId);
        return Result.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除教师")
    @PreAuthorize("hasAuthority('teacher:delete')")
    public Result<Void> delete(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id) {
        Long tenantId = userPrincipal.getTenantId();
        teacherService.delete(id, tenantId);
        return Result.success(null);
    }
}
