package dev.awesomedev.mirim.web;

import dev.awesomedev.mirim.domain.User;
import dev.awesomedev.mirim.service.AuthService;
import dev.awesomedev.mirim.service.ProgressService;
import dev.awesomedev.mirim.web.dto.ToggleResponse;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 이 파일이 하는 일:
 * 체크리스트 진행 상황(내 체크 목록 조회, 체크 토글) API를 제공하는 컨트롤러다.
 */
@RestController
@RequestMapping("/api/progress")
public class ProgressController {

    private final ProgressService progressService;
    private final AuthService authService;

    public ProgressController(ProgressService progressService, AuthService authService) {
        this.progressService = progressService;
        this.authService = authService;
    }

    /** GET /api/progress/me — 내가 체크한 항목 id 배열 */
    @GetMapping("/me")
    public List<Long> myProgress(Authentication authentication) {
        User user = authService.requireUser(authentication);
        return progressService.checkedItemIds(user);
    }

    /** POST /api/progress/{itemId}/toggle — 체크 상태 뒤집기 */
    @PostMapping("/{itemId}/toggle")
    public ToggleResponse toggle(@PathVariable Long itemId, Authentication authentication) {
        User user = authService.requireUser(authentication);
        boolean checked = progressService.toggle(user, itemId);
        return new ToggleResponse(itemId, checked);
    }
}
