package com.goldenchart.entity;

import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;

import java.time.LocalDateTime;

/**
 * GoldenChart 투자전략 DSL 엔티티.
 * frontend StrategyPage 에서 작성한 LogicNode 트리를 JSON 으로 저장.
 */
@Entity
@Table(name = "gc_strategy")
@Getter @Setter @NoArgsConstructor @AllArgsConstructor @Builder
public class GcStrategy {

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

    /** 로그인 사용자 ID (nullable — 비회원은 device_id 사용) */
    @Column(name = "user_id")
    private Long userId;

    /** 비회원 기기 식별자 */
    @Column(name = "device_id", length = 100)
    private String deviceId;

    @Column(name = "name", nullable = false, length = 200)
    private String name;

    @Column(name = "description", columnDefinition = "TEXT")
    private String description;

    /** 매수 조건 DSL JSON — frontend LogicNode 구조 */
    @Column(name = "buy_condition_json", columnDefinition = "JSON")
    @JdbcTypeCode(SqlTypes.JSON)
    private String buyConditionJson;

    /** 매도 조건 DSL JSON — frontend LogicNode 구조 */
    @Column(name = "sell_condition_json", columnDefinition = "JSON")
    @JdbcTypeCode(SqlTypes.JSON)
    private String sellConditionJson;

    @Column(name = "enabled", nullable = false)
    @Builder.Default
    private Boolean enabled = true;

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

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

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

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