package com.pharmacopoeia.entity;

import jakarta.persistence.*;
import lombok.*;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;

@Entity @Table(name = "drugs")
@Data @NoArgsConstructor @AllArgsConstructor @Builder
public class Drug {

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

    @Column(name = "drug_id", unique = true, nullable = false)
    private String drugId;

    @Column(nullable = false)
    private String name;

    @Column(name = "name_en")
    private String nameEn;

    private String pinyin;

    private String category;

    private String subcategory;

    @Column(name = "approval_number")
    private String approvalNumber;

    @Column(columnDefinition = "jsonb")
    @Convert(converter = com.pharmacopoeia.config.JsonbConverter.class)
    private Map<String, Object> sections = new HashMap<>();

    @Column(name = "source_version")
    private String sourceVersion;

    @Column(name = "source_volume")
    private String sourceVolume;

    @Column(name = "source_page")
    private String sourcePage;

    @Column(name = "is_active")
    @Builder.Default
    private Boolean isActive = true;

    @Column(name = "created_at")
    private Instant createdAt;

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

    @PrePersist
    void prePersist() {
        createdAt = Instant.now();
        updatedAt = Instant.now();
    }

    @PreUpdate
    void preUpdate() {
        updatedAt = Instant.now();
    }
}
