package com.goldenchart.entity;

import jakarta.persistence.*;
import lombok.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 백테스팅 설정 엔티티.
 * Ta4j 의 비용 모델·손절/익절/트레일링 스탑·진입가격·재진입 규칙 등을 저장.
 */
@Entity
@Table(name = "gc_backtest_settings")
@Getter @Setter @NoArgsConstructor @AllArgsConstructor @Builder
public class GcBacktestSettings {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "device_id", length = 100)
    private String deviceId;

    // ── 자본 설정 ──────────────────────────────────────────────────────────────
    @Column(name = "initial_capital", nullable = false, precision = 20, scale = 2)
    @Builder.Default
    private BigDecimal initialCapital = new BigDecimal("10000000.00");

    // ── 비용 모델 ──────────────────────────────────────────────────────────────
    /** LINEAR | ZERO */
    @Column(name = "commission_type", nullable = false, length = 20)
    @Builder.Default
    private String commissionType = "LINEAR";

    @Column(name = "commission_rate", nullable = false, precision = 8, scale = 5)
    @Builder.Default
    private BigDecimal commissionRate = new BigDecimal("0.00150");

    @Column(name = "slippage_rate", nullable = false, precision = 8, scale = 5)
    @Builder.Default
    private BigDecimal slippageRate = new BigDecimal("0.00050");

    // ── 진입/청산 가격 ─────────────────────────────────────────────────────────
    /** CLOSE | NEXT_OPEN */
    @Column(name = "entry_price_type", nullable = false, length = 20)
    @Builder.Default
    private String entryPriceType = "CLOSE";

    /** CLOSE | NEXT_OPEN */
    @Column(name = "exit_price_type", nullable = false, length = 20)
    @Builder.Default
    private String exitPriceType = "CLOSE";

    // ── 포지션 방향 ────────────────────────────────────────────────────────────
    /** LONG | SHORT | BOTH */
    @Column(name = "position_direction", nullable = false, length = 10)
    @Builder.Default
    private String positionDirection = "LONG";

    // ── 거래 규모 ──────────────────────────────────────────────────────────────
    /** CAPITAL_PCT | FIXED_AMOUNT */
    @Column(name = "trade_size_type", nullable = false, length = 20)
    @Builder.Default
    private String tradeSizeType = "CAPITAL_PCT";

    @Column(name = "trade_size_value", nullable = false, precision = 10, scale = 4)
    @Builder.Default
    private BigDecimal tradeSizeValue = new BigDecimal("100.0000");

    // ── 손절 ───────────────────────────────────────────────────────────────────
    @Column(name = "stop_loss_enabled", nullable = false)
    @Builder.Default
    private Boolean stopLossEnabled = false;

    @Column(name = "stop_loss_pct", nullable = false, precision = 6, scale = 3)
    @Builder.Default
    private BigDecimal stopLossPct = new BigDecimal("2.000");

    // ── 익절 ───────────────────────────────────────────────────────────────────
    @Column(name = "take_profit_enabled", nullable = false)
    @Builder.Default
    private Boolean takeProfitEnabled = false;

    @Column(name = "take_profit_pct", nullable = false, precision = 6, scale = 3)
    @Builder.Default
    private BigDecimal takeProfitPct = new BigDecimal("5.000");

    // ── 트레일링 스탑 ──────────────────────────────────────────────────────────
    @Column(name = "trailing_stop_enabled", nullable = false)
    @Builder.Default
    private Boolean trailingStopEnabled = false;

    @Column(name = "trailing_stop_pct", nullable = false, precision = 6, scale = 3)
    @Builder.Default
    private BigDecimal trailingStopPct = new BigDecimal("2.000");

    // ── 재진입 제어 ────────────────────────────────────────────────────────────
    @Column(name = "reentry_wait_bars", nullable = false)
    @Builder.Default
    private Integer reentryWaitBars = 0;

    @Column(name = "max_open_trades", nullable = false)
    @Builder.Default
    private Integer maxOpenTrades = 1;

    // ── 포지션 종속성 모드 ─────────────────────────────────────────────────────
    /** LONG_ONLY | SIGNAL_ONLY */
    @Column(name = "position_mode", nullable = false, length = 20)
    @Builder.Default
    private String positionMode = "LONG_ONLY";

    // ── 분할 청산 ──────────────────────────────────────────────────────────────
    @Column(name = "partial_exit_enabled", nullable = false)
    @Builder.Default
    private Boolean partialExitEnabled = false;

    @Column(name = "partial_exit_pct", nullable = false, precision = 6, scale = 3)
    @Builder.Default
    private BigDecimal partialExitPct = new BigDecimal("50.000");

    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;

    @Column(name = "updated_at")
    private LocalDateTime updatedAt;

    @PrePersist
    protected void onCreate() { createdAt = updatedAt = LocalDateTime.now(); }

    @PreUpdate
    protected void onUpdate() { updatedAt = LocalDateTime.now(); }
}
