package com.goldenchart.controller;

import com.goldenchart.entity.GcBacktestResult;
import com.goldenchart.repository.GcBacktestResultRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 백테스팅 실행 이력 조회 API.
 *
 * <pre>
 * GET  /backtest-results?deviceId=xxx     → 이력 목록 (최신순)
 * GET  /backtest-results/{id}             → 단건 상세 조회
 * DELETE /backtest-results/{id}           → 삭제
 * </pre>
 */
@RestController
@RequestMapping("/backtest-results")
@RequiredArgsConstructor
public class BacktestResultController {

    private final GcBacktestResultRepository resultRepository;

    @GetMapping
    public ResponseEntity<List<GcBacktestResult>> list(
            @RequestParam(required = false, defaultValue = "default") String deviceId) {
        return ResponseEntity.ok(resultRepository.findByDeviceIdOrderByCreatedAtDesc(deviceId));
    }

    @GetMapping("/{id}")
    public ResponseEntity<GcBacktestResult> get(@PathVariable Long id) {
        return resultRepository.findById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Long id) {
        if (!resultRepository.existsById(id)) return ResponseEntity.notFound().build();
        resultRepository.deleteById(id);
        return ResponseEntity.noContent().build();
    }
}
