content: /*
   * Copyright 2025 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      https://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package io.agentscope.runtime.engine.memory.persistence.memory.entity;
  
  import jakarta.persistence.*;
  import java.time.LocalDateTime;
  import java.util.List;
  
  /**
   * 会话实体类
   */
  @Entity
  @Table(name = "sessions", indexes = {
      @Index(name = "idx_user_id", columnList = "user_id"),
      @Index(name = "idx_created_at", columnList = "created_at")
  })
  public class SessionEntity {
      
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
      
      @Column(name = "session_id", nullable = false, unique = true, length = 255)
      private String sessionId;
      
      @Column(name = "user_id", nullable = false, length = 255)
      private String userId;
      
      @Column(name = "created_at", nullable = false)
      private LocalDateTime createdAt;
      
      @Column(name = "updated_at")
      private LocalDateTime updatedAt;
      
      @Column(name = "last_activity", nullable = false)
      private LocalDateTime lastActivity;
      
      @OneToMany(mappedBy = "session", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
      private List<SessionMessageEntity> messages;
      
      public SessionEntity() {
          this.createdAt = LocalDateTime.now();
          this.updatedAt = LocalDateTime.now();
          this.lastActivity = LocalDateTime.now();
      }
      
      public SessionEntity(String sessionId, String userId) {
          this();
          this.sessionId = sessionId;
          this.userId = userId;
      }
      
      @PreUpdate
      public void preUpdate() {
          this.updatedAt = LocalDateTime.now();
          this.lastActivity = LocalDateTime.now();
      }
      
      // Getters and Setters
      public Long getId() {
          return id;
      }
      
      public void setId(Long id) {
          this.id = id;
      }
      
      public String getSessionId() {
          return sessionId;
      }
      
      public void setSessionId(String sessionId) {
          this.sessionId = sessionId;
      }
      
      public String getUserId() {
          return userId;
      }
      
      public void setUserId(String userId) {
          this.userId = userId;
      }
      
      public LocalDateTime getCreatedAt() {
          return createdAt;
      }
      
      public void setCreatedAt(LocalDateTime createdAt) {
          this.createdAt = createdAt;
      }
      
      public LocalDateTime getUpdatedAt() {
          return updatedAt;
      }
      
      public void setUpdatedAt(LocalDateTime updatedAt) {
          this.updatedAt = updatedAt;
      }
      
      public LocalDateTime getLastActivity() {
          return lastActivity;
      }
      
      public void setLastActivity(LocalDateTime lastActivity) {
          this.lastActivity = lastActivity;
      }
      
      public List<SessionMessageEntity> getMessages() {
          return messages;
      }
      
      public void setMessages(List<SessionMessageEntity> messages) {
          this.messages = messages;
      }
  }
  