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 = "School - Parent", description = "Parent Management APIs for School")
@RestController
@RequestMapping("/api/school/parents")
@RequiredArgsConstructor
public class SchoolParentController {

    private final ParentService parentService;

    @Operation(summary = "Create parent")
    @PostMapping
    public Result<Parent> createParent(@Valid @RequestBody ParentCreateRequest request) {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        return Result.success(parentService.createParent(tenantId, request));
    }

    @Operation(summary = "Update parent")
    @PutMapping("/{id}")
    public Result<Parent> updateParent(@PathVariable Long id, @RequestBody ParentUpdateRequest request) {
        return Result.success(parentService.updateParent(id, request));
    }

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

    @Operation(summary = "Get parent page")
    @GetMapping
    public Result<PageResult<Parent>> getParentPage(
            @RequestParam(required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String status) {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        Page<Parent> page = parentService.getParentPage(tenantId, pageNum, pageSize, keyword, status);
        return Result.success(PageResult.of(page));
    }

    @Operation(summary = "Delete parent")
    @DeleteMapping("/{id}")
    public Result<Void> deleteParent(@PathVariable Long id) {
        parentService.deleteParent(id);
        return Result.success();
    }

    @Operation(summary = "Reset parent password")
    @PostMapping("/{id}/reset-password")
    public Result<Void> resetPassword(@PathVariable Long id, @RequestParam String newPassword) {
        parentService.resetPassword(id, newPassword);
        return Result.success();
    }

    @Operation(summary = "Bind student to parent")
    @PostMapping("/{parentId}/students/{studentId}")
    public Result<Void> bindStudent(
            @PathVariable Long parentId,
            @PathVariable Long studentId,
            @RequestParam(required = false) String relationship,
            @RequestParam(required = false) Boolean isPrimary) {
        parentService.bindStudent(parentId, studentId, relationship, isPrimary);
        return Result.success();
    }

    @Operation(summary = "Unbind student from parent")
    @DeleteMapping("/{parentId}/students/{studentId}")
    public Result<Void> unbindStudent(@PathVariable Long parentId, @PathVariable Long studentId) {
        parentService.unbindStudent(parentId, studentId);
        return Result.success();
    }

}
