package com.example.springip.lab3.models;

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

import java.util.*;

@Entity
public class Company {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long Id;
    @Column(unique = true)
    private String name;
    @OneToMany(mappedBy = "company", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private Set<Employee> employees = new HashSet<>();

    public Company(String name, Set<Employee> employees) {
        this.name = name;
        this.employees = employees;
    }

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

    public Company() {
    }

    public String getName() {
        return name;
    }

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

    public void addNewEmployee(Employee employee) {
        employees.add(employee);
        employee.setCompany(this);
    }

    public void deleteEmployee(Employee employee) {
        employees.remove(employee);
        employee.deleteCompany();

    }

    public Long getId() {
        return Id;
    }

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

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

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

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

    @Override
    public String toString() {
        return "Company{" +
                "Id=" + Id +
                ", name='" + name + '\'' +
                ", employees=" + employees +
                '}';
    }
}
