package com.competition.modules.pub.controller;

import com.competition.common.result.Result;
import com.competition.common.util.SecurityUtil;
import com.competition.modules.pub.dto.PublicLoginDto;
import com.competition.modules.pub.dto.PublicRegisterDto;
import com.competition.modules.pub.service.PublicAuthService;
import com.competition.security.annotation.Public;
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.Map;

@Tag(name = "公众端认证")
@RestController
@RequestMapping("/public/auth")
@RequiredArgsConstructor
public class PublicAuthController {

    private final PublicAuthService publicAuthService;

    @Public
    @PostMapping("/register")
    @Operation(summary = "公众端注册")
    public Result<Map<String, Object>> register(@Valid @RequestBody PublicRegisterDto dto) {
        return Result.success(publicAuthService.register(dto));
    }

    @Public
    @PostMapping("/login")
    @Operation(summary = "公众端登录")
    public Result<Map<String, Object>> login(@Valid @RequestBody PublicLoginDto dto) {
        return Result.success(publicAuthService.login(dto));
    }

    @PostMapping("/switch-child")
    @Operation(summary = "切换到子女账号")
    public Result<Map<String, Object>> switchChild(@RequestBody Map<String, Long> body) {
        Long parentUserId = SecurityUtil.getCurrentUserId();
        Long childUserId = body.get("childUserId");
        return Result.success(publicAuthService.switchChild(parentUserId, childUserId));
    }
}
