package com.magistr.app.model;

import jakarta.persistence.*;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

@Entity
@Table(name = "schedule_rule_slots")
public class ScheduleRuleSlot {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "schedule_rule_id", nullable = false)
    private ScheduleRule scheduleRule;

    @Column(name = "day_of_week", nullable = false)
    private Integer dayOfWeek;

    @Enumerated(EnumType.STRING)
    @Column(nullable = false, length = 10)
    private ScheduleParity parity;

    @ManyToOne(optional = false)
    @JoinColumn(name = "time_slot_id", nullable = false)
    private TimeSlot timeSlot;

    @ManyToMany
    @JoinTable(
            name = "schedule_rule_slot_subgroups",
            joinColumns = @JoinColumn(name = "schedule_rule_slot_id"),
            inverseJoinColumns = @JoinColumn(name = "subgroup_id")
    )
    private Set<Subgroup> subgroups = new LinkedHashSet<>();

    @ManyToOne(optional = false)
    @JoinColumn(name = "teacher_id", nullable = false)
    private User teacher;

    @ManyToOne(optional = false)
    @JoinColumn(name = "classroom_id", nullable = false)
    private Classroom classroom;

    @ManyToOne(optional = false)
    @JoinColumn(name = "lesson_type_id", nullable = false)
    private LessonType lessonType;

    @Column(name = "lesson_format", nullable = false, length = 30)
    private String lessonFormat;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public ScheduleRule getScheduleRule() {
        return scheduleRule;
    }

    public void setScheduleRule(ScheduleRule scheduleRule) {
        this.scheduleRule = scheduleRule;
    }

    public Integer getDayOfWeek() {
        return dayOfWeek;
    }

    public void setDayOfWeek(Integer dayOfWeek) {
        this.dayOfWeek = dayOfWeek;
    }

    public ScheduleParity getParity() {
        return parity;
    }

    public void setParity(ScheduleParity parity) {
        this.parity = parity;
    }

    public TimeSlot getTimeSlot() {
        return timeSlot;
    }

    public void setTimeSlot(TimeSlot timeSlot) {
        this.timeSlot = timeSlot;
    }

    public Long getSubgroupId() {
        return subgroups.stream().findFirst().map(Subgroup::getId).orElse(null);
    }

    public Subgroup getSubgroup() {
        return subgroups.stream().findFirst().orElse(null);
    }

    public void setSubgroup(Subgroup subgroup) {
        subgroups.clear();
        if (subgroup != null) {
            subgroups.add(subgroup);
        }
    }

    public Set<Subgroup> getSubgroups() {
        return subgroups;
    }

    public void setSubgroups(Set<Subgroup> subgroups) {
        this.subgroups.clear();
        if (subgroups != null) {
            this.subgroups.addAll(subgroups);
        }
    }

    public List<Long> getSubgroupIds() {
        return subgroups.stream()
                .map(Subgroup::getId)
                .toList();
    }

    public List<String> getSubgroupNames() {
        return subgroups.stream()
                .map(Subgroup::getName)
                .toList();
    }

    public User getTeacher() {
        return teacher;
    }

    public void setTeacher(User teacher) {
        this.teacher = teacher;
    }

    public Classroom getClassroom() {
        return classroom;
    }

    public void setClassroom(Classroom classroom) {
        this.classroom = classroom;
    }

    public LessonType getLessonType() {
        return lessonType;
    }

    public void setLessonType(LessonType lessonType) {
        this.lessonType = lessonType;
    }

    public String getLessonFormat() {
        return lessonFormat;
    }

    public void setLessonFormat(String lessonFormat) {
        this.lessonFormat = lessonFormat;
    }
}
