package ai.decompile.ai.model.entity;

import ai.decompile.workspace.model.entity.Binary;
import ai.decompile.workspace.model.entity.Project;
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;
import org.hibernate.annotations.UpdateTimestamp;

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

  @Column(name = "binary_id", nullable = false)
  private UUID binaryId;

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "binary_id", insertable = false, updatable = false)
  private Binary binary;

  @Column(name = "project_id", nullable = false)
  private UUID projectId;

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "project_id", insertable = false, updatable = false)
  private Project project;

  @Column private String title;

  @Column(name = "chat_model", nullable = false, length = 100)
  private String chatModel;

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

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

  @UpdateTimestamp
  @Column(name = "updated_at", nullable = false)
  private Instant updatedAt;
}
