package com.example.springip.lab3.models;

import jakarta.persistence.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Entity
public class Position {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long Id;

    private String name;

    //mappedBy - атрибут, указывающий, что классом-владельцем отношений является другой класс
    @ManyToMany(mappedBy = "positions", cascade = {CascadeType.REMOVE}, fetch = FetchType.EAGER)
    private List<Employee> employees = new ArrayList<>();

    public Position() {
    }

    public Position(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

    public Long getId() {
        return Id;
    }

    public List<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(List<Employee> employees) {
        this.employees = employees;
    }

    public void addNewEmployee(Employee e) {
        employees.add(e);
        if (!e.getPositions().contains(this)) {
            e.addNewPosition(this);
        }
    }
    public void deleteEmployee(Employee e) {
        employees.remove(e);
        if (e.getPositions().contains(this)) {
            e.removePosition(this);
        }
    }

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

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