package com.example.scaffold.entity;

import jakarta.persistence.*;
import lombok.Data;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

/**
 * 用户实体 - 用于演示 JPA 和 MyBatis
 * 
 * 学习要点：
 * 1. @Entity - 标记为 JPA 实体
 * 2. @Table - 指定表名
 * 3. @Id + @GeneratedValue - 主键策略
 * 4. @Column - 列映射
 * 5. @Data - Lombok 自动生成 getter/setter
 */
@Data
@Entity
@Table(name = "users")
@EntityListeners(AuditingEntityListener.class)
public class User {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(nullable = false, length = 50)
    private String username;
    
    @Column(nullable = false, length = 100)
    private String email;
    
    @Column(length = 20)
    private String phone;
    
    @Column(columnDefinition = "TEXT")
    private String bio;
    
    @Column(nullable = false)
    private Boolean active = true;
    
    @CreatedDate
    @Column(name = "created_at", updatable = false)
    private LocalDateTime createdAt;
    
    @LastModifiedDate
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;
    
    /**
     * 生命周期回调 - 持久化前
     */
    @PrePersist
    public void prePersist() {
        System.out.println("🔔 [User @PrePersist] 即将保存用户: " + username);
    }
    
    /**
     * 生命周期回调 - 持久化后
     */
    @PostPersist
    public void postPersist() {
        System.out.println("✅ [User @PostPersist] 用户已保存, ID: " + id);
    }
    
    /**
     * 生命周期回调 - 更新前
     */
    @PreUpdate
    public void preUpdate() {
        System.out.println("🔔 [User @PreUpdate] 即将更新用户: " + id);
    }
    
    /**
     * 生命周期回调 - 更新后
     */
    @PostUpdate
    public void postUpdate() {
        System.out.println("✅ [User @PostUpdate] 用户已更新: " + id);
    }
    
    /**
     * 生命周期回调 - 删除前
     */
    @PreRemove
    public void preRemove() {
        System.out.println("🔔 [User @PreRemove] 即将删除用户: " + id);
    }
    
    /**
     * 生命周期回调 - 删除后
     */
    @PostRemove
    public void postRemove() {
        System.out.println("✅ [User @PostRemove] 用户已删除: " + id);
    }
}