package fr.titionfire.ffsaf.data.model;

import io.quarkus.runtime.annotations.RegisterForReflection;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

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

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@RegisterForReflection

@Entity
@Table(name = "tree")
public class TreeModel {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id;

    @Column(name = "id_category")
    Long category;

    Integer level;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "match_id", referencedColumnName = "id")
    MatchModel match;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
    @JoinColumn(referencedColumnName = "id")
    TreeModel left;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
    @JoinColumn(referencedColumnName = "id")
    TreeModel right;

    public List<TreeModel> flat() {
        List<TreeModel> out = new ArrayList<>();
        this.flat(out);
        return out;
    }

    private void flat(List<TreeModel> out) {
        out.add(this);

        if (this.right != null)
            this.right.flat(out);

        if (this.left != null)
            this.left.flat(out);
    }
}
