import { Column, Entity, JoinColumn, ManyToOne, OneToMany, Relation} from "typeorm";
import { AbstractEntity } from "./AbstractEntity.js";
import { RecipeEntity } from "./RecipeEntity.js";
import { RecipeIngredientEntity } from "./RecipeIngredientEntity.js";

/**
 * Entity describing an ingredient group for a recipe
 */
@Entity({ name: "recipe_ingredient_group" })
export class RecipeIngredientGroupEntity extends AbstractEntity {
    @Column({ nullable: true })
    title?: string;

    @Column({ name: "sort_order", nullable: false })
    sortOrder!: number;

    @JoinColumn({name: "recipe_id"})
    @ManyToOne(() => RecipeEntity, (recipe) => recipe.ingredientGroups,
    {
        onDelete: "CASCADE", 
        nullable: false,
        orphanedRowAction: "delete" // delete removed groups
    }
    )
    recipe!: Relation<RecipeEntity>;

    @OneToMany(() => RecipeIngredientEntity, (ingredient) => ingredient.ingredientGroup,  {
        cascade: true
    })
    ingredients!: Relation<RecipeIngredientEntity>[];
    
}