import { Collection, Entity, Enum, Index, OneToMany, Property } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity';
import { Delivery } from '../../delivery/entities/delivery.entity';
import { PlanEnum } from '../interface/plan.interface';

@Entity({ tableName: 'restaurants' })
@Index({ properties: ['slug', 'isActive'] })
@Index({ properties: ['slug'] })
export class Restaurant extends BaseEntity {
  // --- اطلاعات پایه ---
  @Property()
  name!: string;

  @Property({ unique: true })
  slug!: string;

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

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

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

  // --- مختصات جغرافیایی ---
  @Property({ type: 'decimal', precision: 10, scale: 7, nullable: true })
  latitude?: number;

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

  @Property({ type: 'json', nullable: true })
  serviceArea?: {
    type: 'Polygon';
    coordinates: number[][][];
  };

  // --- وضعیت‌ها ---
  @Property({ default: true })
  isActive: boolean = true;

  @Property({ nullable: true })
  establishedYear?: number;

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

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

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

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

  // --- توضیحات ---
  @Property({ type: 'text', nullable: true })
  description?: string;

  // --- سئو ---
  @Property({ nullable: true })
  seoTitle?: string;

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

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

  // --- تصاویر ---
  @Property({ nullable: true, type: 'json' })
  images?: string[];

  // --- مالیات یا VAT ---
  @Property({ type: 'decimal', default: 0 })
  vat?: number = 0;

  @Property()
  domain!: string;

  // --- روش‌های ارسال ---
  @OneToMany(() => Delivery, delivery => delivery.restaurant)
  deliveries = new Collection<Delivery>(this);

  @Property({ type: 'json', nullable: true })
  score: {
    purchaseAmount: string;
    purchaseScore: string;
    scoreAmount: string;
    scoreCredit: string;
    // bonus score
    birthdayScore: string;
    registerScore: string;
    marriageDateScore: string;
    referrerScore: string;
  } | null = null;

  @Enum(() => PlanEnum)
  plan: PlanEnum = PlanEnum.Base;

  @Property({unique: true,nullable: true})
  subscriptionId?: string;

  @Property({nullable: true})
  subscriptionEndDate?: Date;

  @Property({nullable: true})
  subscriptionStartDate?: Date;

}
