import { Column, Entity, JoinTable, ManyToMany, OneToMany } from 'typeorm'
import { SoftDeleteEntity } from '../entity'
import { Table } from './table.model'
import { Reservation } from '../reservation'

@Entity()
export class TableCombination extends SoftDeleteEntity<TableCombination> {
	/**
	 * Name of the table combination
	 */
	@Column({
		type: 'varchar',
		length: 30,
		nullable: false,
	})
	name: string

	/**
	 * List of tables in the combination
	 */
	@ManyToMany(() => Table)
	@JoinTable({ name: 'table_combination_tables' })
	tables: Table[]

	/**
	 * The reservation for which order was created
	 */
	@OneToMany(() => Reservation, (reservation) => reservation.tableCombination, { nullable: true })
	reservations?: Reservation[]

	get capacity(): number {
		return this.tables?.map((table) => table.capacity).reduce((acc, cur) => acc + cur, 0) ?? 0
	}
}
