import { TablePriority } from '@core/types'
import { Column, Entity, OneToMany } from 'typeorm'
import { SoftDeleteEntity } from '../entity'
import { Box, Point } from './box.model'
import { TableShape } from './enums'
import { TableSubscriber } from './table-subscriber.model'

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

	/**
	 * Maximum no of people table can hold
	 */
	@Column({ type: 'integer', nullable: false })
	capacity: number

	/**
	 * Width of the table as displayed on room plan pages
	 */
	@Column({ type: 'integer', nullable: false })
	width: number

	/**
	 * Height of the table as displayed on room plan pages
	 */
	@Column({ type: 'integer', nullable: false })
	height: number

	/**
	 * Position at which table is located on X axis
	 */
	@Column({ type: 'integer', nullable: false })
	columnStart: number

	/**
	 * Position at which table is located on Y axis
	 */
	@Column({ type: 'integer', nullable: false })
	rowStart: number

	@Column({
		type: 'enum',
		enum: TablePriority,
		default: TablePriority.normal,
		nullable: false,
	})
	priority: TablePriority

	/**
	 * Shape of the table
	 */
	@Column({
		type: 'enum',
		enum: TableShape,
		default: TableShape.rectangle,
		nullable: false,
	})
	shape: TableShape

	/**
	 * List of table subscribers
	 */
	@OneToMany(() => TableSubscriber, (subscriber) => subscriber.table)
	subscribers: TableSubscriber[]

	/**
	 * Returns the box that represents the table inside the grid
	 */
	get box(): Box {
		return new Box(
			new Point(this.columnStart, this.rowStart),
			new Point(this.columnStart + this.width, this.rowStart + this.height),
		)
	}
}
