package com.jasamedika.medifirst2000.entities;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.jasamedika.medifirst2000.base.BaseMaster;
import com.jasamedika.medifirst2000.helper.Caption;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.HashSet;
import java.util.Set;

import static javax.persistence.CascadeType.ALL;
import static javax.persistence.FetchType.LAZY;
import static javax.persistence.GenerationType.SEQUENCE;

@Getter
@Setter
@NoArgsConstructor
@Entity
@Table(name = "loginuser_s")
public class LoginUser extends BaseMaster {

	public LoginUser(String kataSandi, KelompokUser kelompokUser, String namaUser, String namaLengkap,
			String namaPanggilan, String nikIntern, String nipPns, String noIdentitas, String npwp, Integer id,
			String email, Integer ruanganId, String namaRuangan, String kdDepartemen, String namaDepartemen,
			Integer departemenId) {
		super();
		this.kataSandi = kataSandi;
		this.kelompokUser = kelompokUser;
		this.namaUser = namaUser;
		this.pegawai = new Pegawai(namaLengkap, namaPanggilan, nikIntern, nipPns, noIdentitas, npwp, id, email,
				ruanganId, namaRuangan, kdDepartemen, namaDepartemen, departemenId);
	}

	public LoginUser(Integer idLoginUser, String kataSandi, KelompokUser kelompokUser, String namaUser,
			String namaLengkap, String namaPanggilan, String nikIntern, String nipPns, String noIdentitas, String npwp,
			Integer id, String email, Integer ruanganId, String namaRuangan, String kdDepartemen, String namaDepartemen,
			Integer departemenId) {
		super();
		this.id = idLoginUser;
		this.kataSandi = kataSandi;
		this.kelompokUser = kelompokUser;
		this.namaUser = namaUser;
		this.pegawai = new Pegawai(namaLengkap, namaPanggilan, nikIntern, nipPns, noIdentitas, npwp, id, email,
				ruanganId, namaRuangan, kdDepartemen, namaDepartemen, departemenId);
	}

	public LoginUser(Integer idLoginUser, KelompokUser kelompokUser, String namaUser, String namaLengkap,
			String namaPanggilan, String nikIntern, String nipPns, String noIdentitas, String npwp, Integer id,
			String email, Integer ruanganId, String namaRuangan, String kdDepartemen, String namaDepartemen,
			Integer departemenId) {
		super();
		this.id = idLoginUser;
		this.kelompokUser = kelompokUser;
		this.namaUser = namaUser;
		this.pegawai = new Pegawai(namaLengkap, namaPanggilan, nikIntern, nipPns, noIdentitas, npwp, id, email,
				ruanganId, namaRuangan, kdDepartemen, namaDepartemen, departemenId);
	}

	public LoginUser(Integer idLoginUser, String namaUser) {
		super();
		this.id = idLoginUser;
		this.namaUser = namaUser;
	}

	@Id
	@GeneratedValue(strategy = SEQUENCE, generator = "public.LoginUser_S_id_seq")
	@SequenceGenerator(name = "public.LoginUser_S_id_seq", sequenceName = "public.LoginUser_S_id_seq", allocationSize = 1)
	@Column(name = "id")
	protected Integer id;

	@NotNull(message = "Kata Sandi tidak boleh kosong")
	@Column(name = "KataSandi", nullable = false, length = 50)
	@Caption(value = "Kata Sandi")
	private String kataSandi;

	@ManyToOne(fetch = LAZY)
	@JoinColumn(name = "ObjectKelompokUserFk")
	@Caption(value = "Object Kelompok User")
	private KelompokUser kelompokUser;

	@Column(name = "ObjectKelompokUserFk", insertable = false, updatable = false)
	private Integer kelompokUserId;

	@ManyToOne(fetch = LAZY)
	@JoinColumn(name = "ObjectPegawaiFk")
	@NotNull(message = "Kd Pegawai tidak boleh kosong")
	@Caption(value = "Object Pegawai")
	private Pegawai pegawai;

	@Column(name = "ObjectPegawaiFk", insertable = false, updatable = false, nullable = false)
	private Integer pegawaiId;

	@NotNull(message = "Nama User tidak boleh kosong")
	@Column(name = "NamaUser", nullable = false, length = 40, unique = true)
	@Caption(value = "Nama User")
	private String namaUser;

	@NotNull(message = "Status Login tidak boleh kosong")
	@Column(name = "StatusLogin", nullable = false)
	@Caption(value = "Status Login")
	private Byte statusLogin;

	@JsonBackReference
	@OneToMany(cascade = ALL, fetch = LAZY, mappedBy = "loginUser")
	private Set<MapLoginUserToRuangan> mapLoginUserToRuanganSet = new HashSet<>();

}
