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

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

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

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

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

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

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

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

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

  @CreateDateColumn()
  createTime: Date;

  @UpdateDateColumn()
  updateTime: Date;

  @OneToMany(() => CategoryTranslation, (articleTranslation) => articleTranslation.source, {
    cascade: true,
  })
  translations?: Translation<CategoryTranslation>[] | undefined;

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