package cn.reghao.tnb.search.app.model.po;

import cn.reghao.tnb.search.app.util.BaseEntity;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.search.engine.backend.types.Highlightable;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.FullTextField;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed;

import jakarta.persistence.*;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2025-08-21 09:29:18
 */
@NoArgsConstructor
@Setter
@Getter
@Indexed(index = "wenshu_doc")
@Entity
@Table(name = "search_wenshu_doc")
public class WenshuDoc extends BaseEntity {
    @Column(unique = true)
    private long wenshuId;
    // 原始链接
    private String originalUrl;
    // 案号
    private String caseId;
    // 案件名称
    @FullTextField(analyzer = "ikAnalyzer", highlightable = Highlightable.ANY)
    private String caseName;
    // 法院
    private String court;
    // 所属地区
    private String region;
    // 案件类型
    private String caseType;
    // 案件类型编码
    private String caseTypeId;
    // 审理程序
    // procedure 触发 mysql 关键字
    private String procedures;
    // 裁判日期
    private String judgmentDate;
    // 发布日期
    private String publicDate;
    // 当事人
    @Column(columnDefinition = "text")
    private String parties;
    // 案由
    private String cause;
    // 法律依据
    private transient String legalBasis;
    /*@ElementCollection(targetClass = WenshuLegal.class)
    @LazyCollection(LazyCollectionOption.FALSE)
    @CollectionTable(name = "search_wenshu_legals")*/
    @Transient
    private List<WenshuLegal> legalBasisList;

    public WenshuDoc(long wenshuId, Wenshu wenshu) {
        this.wenshuId = wenshuId;
        this.originalUrl = wenshu.getOriginalUrl();
        this.caseId = wenshu.getCaseId();
        this.caseName = wenshu.getCaseName();
        this.court = wenshu.getCourt();
        this.region = wenshu.getRegion();
        this.caseType = wenshu.getCaseType();
        this.caseTypeId = wenshu.getCaseTypeId();
        this.procedures = wenshu.getProcedure();
        this.judgmentDate = wenshu.getJudgmentDate();
        this.publicDate = wenshu.getPublicDate();
        this.parties = wenshu.getParties();
        this.cause = wenshu.getCause();
        // this.legalBasis = wenshu.getLegalBasis();
        /*this.legalBasisList = Arrays.stream(wenshu.getLegalBasis().split("；"))
                .map(WenshuLegal::new)
                .filter(wenshuLegal -> !wenshuLegal.getLegalBasis().isBlank())
                .collect(Collectors.toList());*/
    }
}
