package com.goldenchart.controller;

import com.goldenchart.dto.TradeSignalDto;
import com.goldenchart.service.TradeSignalService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 매매 시그널 이력 REST API.
 *
 * <pre>
 * GET  /api/trade-signals          → 전체 이력
 * GET  /api/trade-signals?market=  → 종목별 이력
 * </pre>
 */
@RestController
@RequestMapping("/trade-signals")
@RequiredArgsConstructor
public class TradeSignalController {

    private final TradeSignalService service;

    @GetMapping
    public ResponseEntity<List<TradeSignalDto>> list(
            @RequestParam(required = false) String market,
            @RequestHeader(value = "X-User-Id",   required = false) String userIdHeader,
            @RequestHeader(value = "X-Device-Id", required = false) String deviceId) {

        Long userId = parseUserId(userIdHeader);
        List<TradeSignalDto> result = (market != null && !market.isBlank())
                ? service.listByMarket(userId, deviceId, market)
                : service.list(userId, deviceId);
        return ResponseEntity.ok(result);
    }

    private Long parseUserId(String h) {
        if (h == null || h.isBlank()) return null;
        try { return Long.parseLong(h); } catch (NumberFormatException e) { return null; }
    }
}
