package ai.decompile.analysis.model.entity;

import jakarta.persistence.*;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;

@Entity
@Table(name = "static_function")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class StaticFunction {
  @Id
  @GeneratedValue(strategy = GenerationType.UUID)
  private UUID id;

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "analysis_id", nullable = false)
  private StaticAnalysis analysis;

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

  @Column(nullable = false, length = 20)
  private String address;

  @Column(length = 100)
  private String flags;

  @Column(name = "function_size")
  private Integer functionSize;

  @Column(columnDefinition = "TEXT")
  private String assembly;

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

  @Column(length = 1000)
  private String signature;

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

  @Column(columnDefinition = "TEXT")
  private String comments;

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

  @OneToMany(
      mappedBy = "caller",
      cascade = CascadeType.ALL,
      orphanRemoval = true,
      fetch = FetchType.LAZY)
  @Builder.Default
  private List<StaticXref> calleeXrefs = new ArrayList<>();

  @OneToMany(
      mappedBy = "callee",
      cascade = CascadeType.ALL,
      orphanRemoval = true,
      fetch = FetchType.LAZY)
  @Builder.Default
  private List<StaticXref> callerXrefs = new ArrayList<>();

  @CreationTimestamp
  @Column(name = "created_at", nullable = false, updatable = false)
  private Instant createdAt;
}
