package com.nbport.zgwl.controller;

import com.nbport.zgwl.common.PageResult;
import com.nbport.zgwl.common.R;
import com.nbport.zgwl.dto.StudentQueryDto;
import com.nbport.zgwl.dto.StudentSaveDto;
import com.nbport.zgwl.entity.StudentEntity;
import com.nbport.zgwl.entity.SysUserEntity;
import com.nbport.zgwl.service.StudentService;
import com.nbport.zgwl.utils.MockSecurityManager;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/student")
@Slf4j
@RequiredArgsConstructor
@Validated
public class StudentController {

    private final StudentService studentService;
    private final MockSecurityManager securityManager;

    @PostMapping("/list")
    public R<PageResult<Map<String, Object>>> queryList(@RequestBody StudentQueryDto queryDto) {
        SysUserEntity user = securityManager.getLoginUser();
        PageResult<Map<String, Object>> pageResult = studentService.queryList(queryDto, user);
        return R.success(pageResult);
    }

    @GetMapping("/detail/{id}")
    public R<StudentEntity> detail(@PathVariable("id") Long id) {
        return R.success(studentService.detail(id));
    }

    @PostMapping("/add")
    public R<String> add(@RequestBody @Valid StudentSaveDto dto) {
        log.info("新增学生请求：{}", dto);
        studentService.add(dto);
        return R.success("新增成功");
    }

    @PostMapping("/update")
    public R<String> update(@RequestBody @Valid StudentSaveDto dto) {
        log.info("修改学生请求：{}", dto);
        studentService.update(dto);
        return R.success("修改成功");
    }

    @PostMapping("/delete/{id}")
    public R<String> delete(@PathVariable("id") Long id) {
        studentService.delete(id);
        return R.success("删除成功");
    }
}
