package ru.ulstu.is.cbapp.models;

import javax.persistence.*;

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

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

    private String name;
    @OneToMany(mappedBy = "drivingSchool", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private List<Student> students = new ArrayList<>();

    public DrivingSchool(String name, List<Student> students) {
        this.name = name;
        this.students = students;
    }

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

    public DrivingSchool() {
    }

    public String getName() {
        return name;
    }

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

    public void addNewStudent(Student student) {
        students.add(student);
        student.setDrivingSchool(this);
    }

    public void deleteStudent(Student student) {
        students.remove(student);
        student.deleteDrivingSchool();

    }

    public Long getId() {
        return Id;
    }

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }

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

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

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