package com.lanyuanxiaoyao.leopard.core.entity;

import com.lanyuanxiaoyao.leopard.core.Constants;
import com.lanyuanxiaoyao.leopard.core.entity.base.SimpleEnum;
import com.lanyuanxiaoyao.service.template.entity.SimpleEntity;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.time.LocalDate;
import java.util.Set;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.FieldNameConstants;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

/**
 * 股票
 *
 * @author lanyuanxiaoyao
 * @version 20250828
 */
@Setter
@Getter
@ToString(callSuper = true)
@FieldNameConstants
@Entity
@DynamicUpdate
@DynamicInsert
@EntityListeners(AuditingEntityListener.class)
@Table(name = Constants.DATABASE_PREFIX + "stock")
public class Stock extends SimpleEntity {
  @Column(unique = true, nullable = false)
  @Comment("股票代码")
  private String code;
  @Column(nullable = false)
  @Comment("股票名称")
  private String name;
  @Column(nullable = false)
  @Comment("股票全称")
  private String fullname;
  @Column(nullable = false)
  @Comment("交易市场")
  @Enumerated(EnumType.STRING)
  private Market market;
  @Comment("行业")
  private String industry;
  @Comment("上市日期")
  private LocalDate listedDate;

  @OneToMany(mappedBy = "stock", cascade = CascadeType.REMOVE)
  @ToString.Exclude
  private Set<Daily> dailies;

  @OneToMany(mappedBy = "stock", cascade = CascadeType.REMOVE)
  @ToString.Exclude
  private Set<Yearly> yearlies;

  @OneToMany(mappedBy = "stock", cascade = CascadeType.REMOVE)
  @ToString.Exclude
  private Set<FinanceIndicator> indicators;

  @Override
  public boolean equals(Object o) {
    if (o == null || getClass() != o.getClass()) return false;

    Stock stock = (Stock) o;
    return code.equals(stock.code);
  }

  @Override
  public int hashCode() {
    return code.hashCode();
  }

  @Getter
  @AllArgsConstructor
  public enum Market implements SimpleEnum {
    SSE("上交所"),
    SZSE("深交所"),
    BSE("北交所");

    private final String chineseName;
  }
}
