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 { ArticleTranslation } from './article.entity.translation';
import { TranslatableEntity, Translation } from 'typeorm-translatable';
import { Transform } from 'class-transformer';

@Entity()
export class Article extends TranslatableEntity<ArticleTranslation> {
  @PrimaryGeneratedColumn()
  id: number;

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

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

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

  @Column({ type: 'longtext', nullable: true })
  content?: string;

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

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

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

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

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

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

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

  @CreateDateColumn()
  createTime: Date;

  @UpdateDateColumn()
  updateTime: Date;

  @OneToMany(() => ArticleTranslation, (articleTranslation) => articleTranslation.source, {
    cascade: ['insert', 'update', 'remove'],
  })
  translations?: Translation<ArticleTranslation>[] | undefined;

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