package com.red.circle.other.infra.database.mongo.entity.team.team;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.red.circle.other.inner.model.dto.agency.agency.TeamContact;
import com.red.circle.other.inner.model.dto.agency.agency.TeamCounter;
import com.red.circle.other.inner.model.dto.agency.agency.TeamCountry;
import com.red.circle.other.inner.model.dto.agency.agency.TeamRemark;
import com.red.circle.other.inner.model.dto.agency.agency.TeamSetting;
import com.red.circle.other.inner.model.dto.agency.agency.TeamStatus;
import java.io.Serial;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.List;
import java.util.Objects;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

/**
 * 团队资料.
 *
 * @author pengliang on 2022/12/1
 */
@Data
@Accessors(chain = true)
@Document("team_profile")
public class TeamProfile implements Serializable {

  @Serial
  private static final long serialVersionUID = 1L;

  /**
   * 团队id.
   */
  @Id
  @JsonSerialize(using = ToStringSerializer.class)
  private Long id;

  /**
   * 区域.
   */
  private String region;

  /**
   * 团队系统.
   */
  private String sysOrigin;

  /**
   * 团队拥有者ID.
   */
  private Long ownUserId;

  /**
   * 备注.
   */
  private List<TeamRemark> remarks;

  /**
   * 状态: 正常/关闭.
   */
  private TeamStatus status;

  /**
   * 团队设置.
   */
  private TeamSetting setting;

  /**
   * 国家.
   */
  private TeamCountry country;

  /**
   * 计数器.
   */
  private TeamCounter counter;

  /**
   * 联系方式.
   */
  private List<TeamContact> contacts;

  /**
   * 创建时间.
   */
  private Timestamp createTime;

  /**
   * 修改时间.
   */
  private Timestamp updateTime;

  /**
   * 创建用户.
   */
  @JsonSerialize(using = ToStringSerializer.class)
  private Long createUser;

  /**
   * 创建用户来源：0 app、1 后台.
   */
  private Integer createUserOrigin;

  /**
   * 修改用户.
   */
  @JsonSerialize(using = ToStringSerializer.class)
  private Long updateUser;

  /**
   * 修改用户来源：0 app、1 后台.
   */
  private Integer updateUserOrigin;

  /**
   * true 允许加入、false 不允许加入.
   */
  public boolean checkAllowJoinTeam() {
    return Objects.nonNull(counter)
        && Objects.nonNull(setting)
        && counter.getMemberQuantity() < setting.getMaxMember();
  }

}
