package com.lesingle.creation.controller;

import com.lesingle.creation.common.core.Result;
import com.lesingle.creation.common.security.UserPrincipal;
import com.lesingle.creation.dto.student.CreateStudentDTO;
import com.lesingle.creation.dto.student.UpdateStudentDTO;
import com.lesingle.creation.service.StudentService;
import com.lesingle.creation.vo.student.StudentVO;
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/students")
@RequiredArgsConstructor
public class StudentController {

    private final StudentService studentService;

    @PostMapping
    @Operation(summary = "创建学生")
    @PreAuthorize("hasAuthority('student:create')")
    public Result<StudentVO> create(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @RequestBody @Valid CreateStudentDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        Long creatorId = userPrincipal.getUserId();
        StudentVO result = studentService.create(dto, tenantId, creatorId);
        return Result.success(result);
    }

    @GetMapping
    @Operation(summary = "查询学生列表")
    @PreAuthorize("hasAuthority('student:read')")
    public Result<List<StudentVO>> list(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Long classId) {
        Long tenantId = userPrincipal.getTenantId();
        List<StudentVO> result = studentService.list(tenantId, page, pageSize, classId);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取学生详情")
    @PreAuthorize("hasAuthority('student:read')")
    public Result<StudentVO> getDetail(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id) {
        Long tenantId = userPrincipal.getTenantId();
        StudentVO result = studentService.getDetail(id, tenantId);
        return Result.success(result);
    }

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

    @PutMapping("/{id}")
    @Operation(summary = "更新学生")
    @PreAuthorize("hasAuthority('student:update')")
    public Result<StudentVO> update(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id,
            @RequestBody @Valid UpdateStudentDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        Long modifierId = userPrincipal.getUserId();
        StudentVO result = studentService.update(id, dto, tenantId, modifierId);
        return Result.success(result);
    }

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