package com.ecep.contract.model;

import java.io.Serializable;
import java.time.LocalDate;
import java.util.Objects;

import com.ecep.contract.util.HibernateProxyUtils;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@jakarta.persistence.Entity
@Table(name = "EMPLOYEE", schema = "supplier_ms")
public class Employee implements BasedEntity, IdentityEntity, NamedEntity, Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID", nullable = false)
    private Integer id;

    @Column(name = "ACCOUNT")
    private String account;

    @Column(name = "NAME")
    private String name;

    @Column(name = "ALIAS")
    private String alias;

    @JoinColumn(name = "DEPARTMENT_ID")
    @ManyToOne(fetch = FetchType.LAZY)
    @ToString.Exclude
    @JsonIgnoreProperties({ "leader" })
    private Department department;

    /**
     * U8系统中的人员代码
     */
    @Column(name = "CODE")
    private String code;

    @Column(name = "EMAIL")
    private String email;

    @Column(name = "PHONE")
    private String phone;

    @Column(name = "CREATED")
    private LocalDate created;

    // 新增的入职日期字段
    @Column(name = "ENTRY_DATE")
    private LocalDate entryDate;

    // 新增的离职日期字段
    @Column(name = "LEAVE_DATE")
    private LocalDate leaveDate;

    @Column(name = "LOCALE")
    private String locale;

    @Column(name = "DATE_FORMATTER")
    private String dateFormatter;

    @Column(name = "DATETIME_FORMATTER")
    private String dateTimeFormatter;

    @Column(name = "TIME_FORMATTER")
    private String timeFormatter;

    @Column(name = "TIME_ZONE")
    private String timeZone;

    @Column(name = "NUMBER_FORMATTER")
    private String numberFormatter;

    @Column(name = "CURRENCY_FORMATTER")
    private String currencyFormatter;

    /**
     * 员工是否活跃的状态
     */
    @Column(name = "IS_ACTIVE", nullable = false)
    private boolean isActive;

    @ManyToMany(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
    @JoinTable(name = "EMPLOYEE_ROLES", joinColumns = @JoinColumn(name = "EMPLOYEE_ID"), inverseJoinColumns = @JoinColumn(name = "ROLE_ID"))
    @ToString.Exclude
    @JsonIgnore
    private java.util.List<EmployeeRole> roles = new java.util.ArrayList<>();

    @Override
    public String toPrettyString() {
        if (code == null) {
            return name;
        }
        return code + " " + name;
    }

    @Override
    public final boolean equals(Object object) {
        if (this == object)
            return true;
        if (object == null || HibernateProxyUtils.isNotEffectiveClassEquals(this, object))
            return false;
        Employee employee = (Employee) object;
        return getId() != null && Objects.equals(getId(), employee.getId());
    }

    @Override
    public final int hashCode() {
        return HibernateProxyUtils.hashCode(this);
    }
}
