package com.competition.modules.pub.controller;

import com.competition.common.result.Result;
import com.competition.common.util.SecurityUtil;
import com.competition.modules.pub.dto.CreateChildDto;
import com.competition.modules.pub.service.PublicProfileService;
import com.competition.modules.user.entity.UserChild;
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.*;

import java.util.List;
import java.util.Map;

@Tag(name = "子女管理")
@RestController
@RequestMapping("/public")
@RequiredArgsConstructor
public class PublicChildController {

    private final PublicProfileService publicProfileService;

    @GetMapping("/mine/children")
    @Operation(summary = "获取子女列表")
    public Result<List<UserChild>> getChildren() {
        Long userId = SecurityUtil.getCurrentUserId();
        return Result.success(publicProfileService.getChildren(userId));
    }

    @PostMapping("/mine/children")
    @Operation(summary = "添加子女信息")
    public Result<UserChild> createChild(@Valid @RequestBody CreateChildDto dto) {
        Long userId = SecurityUtil.getCurrentUserId();
        return Result.success(publicProfileService.createChild(userId, dto));
    }

    @GetMapping("/mine/children/{id}")
    @Operation(summary = "获取子女详情")
    public Result<UserChild> getChild(@PathVariable Long id) {
        Long userId = SecurityUtil.getCurrentUserId();
        return Result.success(publicProfileService.getChild(id, userId));
    }

    @PutMapping("/mine/children/{id}")
    @Operation(summary = "更新子女信息")
    public Result<Void> updateChild(@PathVariable Long id, @RequestBody CreateChildDto dto) {
        Long userId = SecurityUtil.getCurrentUserId();
        publicProfileService.updateChild(id, userId, dto);
        return Result.success();
    }

    @DeleteMapping("/mine/children/{id}")
    @Operation(summary = "删除子女信息")
    public Result<Void> deleteChild(@PathVariable Long id) {
        Long userId = SecurityUtil.getCurrentUserId();
        publicProfileService.deleteChild(id, userId);
        return Result.success();
    }

    @PostMapping("/children/create-account")
    @Operation(summary = "创建子女独立账号")
    public Result<Map<String, Object>> createChildAccount(@RequestBody Map<String, String> body) {
        Long parentUserId = SecurityUtil.getCurrentUserId();
        return Result.success(publicProfileService.createChildAccount(
                parentUserId,
                body.get("username"),
                body.get("password"),
                body.get("nickname"),
                body.get("gender"),
                body.get("city"),
                body.get("avatar")));
    }

    @GetMapping("/children/accounts")
    @Operation(summary = "获取子女账号列表")
    public Result<List<Map<String, Object>>> getChildAccounts() {
        Long parentUserId = SecurityUtil.getCurrentUserId();
        return Result.success(publicProfileService.getChildAccounts(parentUserId));
    }

    @PutMapping("/children/accounts/{id}")
    @Operation(summary = "更新子女账号")
    public Result<Void> updateChildAccount(@PathVariable Long id, @RequestBody Map<String, Object> body) {
        Long parentUserId = SecurityUtil.getCurrentUserId();
        publicProfileService.updateChildAccount(id, parentUserId, body);
        return Result.success();
    }
}
