import {
  Column,
  CreateDateColumn,
  Entity,
  JoinColumn,
  JoinTable,
  ManyToMany,
  ManyToOne,
  OneToMany,
  OneToOne,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';
import { Category } from '../category/category.entity';
import { User } from '../user/user.entity';
import { Article } from '../article/article.entity';
import { MenuTranslation } from './menu.entity.translation';
import { TranslatableEntity, Translation } from 'typeorm-translatable';
@Entity()
export class Menu extends TranslatableEntity<MenuTranslation> {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ unique: false, default: '', length: 200 })
  title: string;

  @Column({ default: true })
  enable: boolean;

  @Column({ default: 0 })
  styleType: number;

  @Column({ default: 0 })
  otherType: number;

  @Column({ default: 0 })
  order: number;

  @Column({ default: '' })
  description: string;

  @Column({ default: 0 })
  level: number;

  @Column({ default: '' })
  cover: string;

  @Column({ default: false })
  isPublish: boolean;

  @Column({ default: '' })
  remark: string;

  @OneToOne(() => Category, {
    cascade: false,
    onDelete: 'CASCADE',
    createForeignKeyConstraints: false,
  })
  @JoinColumn()
  category: Category;

  @Column({ nullable: true })
  categoryId: number;

  @OneToOne(() => User, {
    createForeignKeyConstraints: false,
    onDelete: 'CASCADE',
  })
  @JoinColumn()
  user: User;

  @Column({ nullable: true })
  userId: number;

  @CreateDateColumn()
  createTime: Date;

  @UpdateDateColumn()
  updateTime: Date;

  @ManyToOne(() => Menu, (menu) => menu.children, {
    createForeignKeyConstraints: false,
  })
  parent: Menu;

  @OneToMany(() => Menu, (menu) => menu.parent, {
    createForeignKeyConstraints: false,
  })
  children: Menu[];

  @Column({ nullable: true })
  parentId: number;

  @Column({ nullable: true, default: 3 })
  grid: number;

  @OneToOne(() => Article, {
    createForeignKeyConstraints: false,
    onDelete: 'CASCADE',
  })
  @JoinColumn()
  article: Article;

  @Column({ nullable: true })
  articleId: number;
  @OneToMany(() => MenuTranslation, (menuTranslation) => menuTranslation.source, {
    cascade: true,
  })
  translations?: Translation<MenuTranslation>[] | undefined;

  static translatableFields = new Set(['title', 'description']);
}
