import { Cascade, Collection, Entity, Index, ManyToOne, OneToMany, Property, OneToOne } from '@mikro-orm/core';
import { Category } from './category.entity';
import { BaseEntity } from '../../../common/entities/base.entity';
import { Restaurant } from '../../../modules/restaurants/entities/restaurant.entity';
import { Review } from 'src/modules/review/entities/review.entity';
import { Inventory } from 'src/modules/inventory/entities/inventory.entity';
import { MealType } from '../interface/food.interface';
import { Favorite } from './favorite.entity';

@Entity({ tableName: 'foods' })
@Index({ properties: ['restaurant', 'isActive'] })
export class Food extends BaseEntity {
  @ManyToOne(() => Restaurant)
  restaurant: Restaurant;

  @ManyToOne(() => Category)
  category: Category;

  @OneToMany(() => Review, review => review.food, { cascade: [Cascade.ALL], orphanRemoval: true })
  reviews = new Collection<Review>(this);

  @OneToOne(() => Inventory, {
    mappedBy: 'food',
    nullable: true,
  })
  inventory?: Inventory;

  @OneToMany(() => Favorite, favorite => favorite.food)
  favorites = new Collection<Favorite>(this);

  @Property({ nullable: true })
  title?: string;

  @Property({ type: 'text', nullable: true })
  desc?: string;

  @Property({ type: 'json', nullable: true })
  content?: string[];

  @Property({ type: 'decimal', precision: 10, scale: 2, nullable: true })
  price?: number;

  @Property({ type: 'int', nullable: true })
  order?: number;

  @Property({ type: 'int', nullable: true })
  prepareTime?: number; // in minutes

  @Property({ type: 'jsonb', default: [] })
  weekDays: number[] = [0, 1, 2, 3, 4, 5, 6];

  @Property({ type: 'jsonb', default: [] })
  mealTypes: MealType[] = [];

  @Property({ type: 'boolean', default: true })
  isActive: boolean = true;

  @Property({ type: 'json', nullable: true })
  images?: string[];

  @Property({ type: 'boolean', default: false })
  inPlaceServe: boolean = false;

  @Property({ type: 'boolean', default: false })
  pickupServe: boolean = false;

  @Property({ type: 'float', default: null })
  score?: number | null = null;

  @Property({ type: 'float', default: 0 })
  discount: number = 0;

  @Property({ type: 'boolean', default: false })
  isSpecialOffer: boolean = false;
}
