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.ParentCreateRequest;
import com.reading.platform.dto.request.ParentUpdateRequest;
import com.reading.platform.entity.Parent;
import com.reading.platform.service.ParentService;
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/parents")
@RequiredArgsConstructor
public class SchoolParentController {

    private final ParentService parentService;

    @Operation(summary = "创建家长")
    @PostMapping
    public Result<Parent> createParent(@Valid @RequestBody ParentCreateRequest request) {
        String tenantId = SecurityUtils.getCurrentTenantId();
        return Result.success(parentService.createParent(tenantId, request));
    }

    @Operation(summary = "更新家长")
    @PutMapping("/{id}")
    public Result<Parent> updateParent(@PathVariable String id, @RequestBody ParentUpdateRequest request) {
        return Result.success(parentService.updateParent(id, request));
    }

    @Operation(summary = "Get parent by ID")
    @GetMapping("/{id}")
    public Result<Parent> getParent(@PathVariable String id) {
        return Result.success(parentService.getParentById(id));
    }

    @Operation(summary = "获取家长分页")
    @GetMapping
    public Result<PageResult<Parent>> getParentPage(
            @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<Parent> page = parentService.getParentPage(tenantId, pageNum, pageSize, keyword, status);
        return Result.success(PageResult.of(page));
    }

    @Operation(summary = "删除家长")
    @DeleteMapping("/{id}")
    public Result<Void> deleteParent(@PathVariable String id) {
        parentService.deleteParent(id);
        return Result.success();
    }

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

    @Operation(summary = "绑定学生到家长")
    @PostMapping("/{parentId}/students/{studentId}")
    public Result<Void> bindStudent(
            @PathVariable String parentId,
            @PathVariable String studentId,
            @RequestParam(required = false) String relationship,
            @RequestParam(required = false) Boolean isPrimary) {
        parentService.bindStudent(parentId, studentId, relationship, isPrimary);
        return Result.success();
    }

    @Operation(summary = "解绑家长与学生关系")
    @DeleteMapping("/{parentId}/students/{studentId}")
    public Result<Void> unbindStudent(@PathVariable String parentId, @PathVariable String studentId) {
        parentService.unbindStudent(parentId, studentId);
        return Result.success();
    }

}
