package com.example.springip.lab3.models;

import com.fasterxml.jackson.annotation.JsonInclude;
import jakarta.persistence.*;

import java.util.*;

@Entity
public class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String phoneNumber;
    private String name;
    private String surname;

    @ManyToOne(fetch = FetchType.EAGER)
    private Company company;

    @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private Set<Position> positions = new HashSet<>();


    public Employee(String surname, String name, String phoneNumber) {
        this.phoneNumber = phoneNumber;
        this.name = name;
        this.surname = surname;
    }

    public Employee() {
    }

    public Long getId() {
        return id;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public List<Position> getPositions() {
        return positions.stream().toList();
    }

    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
        if (!company.getEmployees().contains(this)) {
            company.addNewEmployee(this);
        }
    }

    public void deleteCompany() {
        if (company.getEmployees().contains(this)) {
            company.deleteEmployee(this);
        }
        this.company = null;
        this.positions.clear();
    }

    public void addNewPosition(Position p) {
        positions.add(p);
        if (!p.getEmployees().contains(this)) {
            p.addNewEmployee(this);
        }

    }

    public void removePosition(Position p) {
        positions.remove(p);
        if (p.getEmployees().contains(this)) {
            p.deleteEmployee(this);
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee e = (Employee) o;
        return Objects.equals(id, e.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}
