import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  ManyToOne,
  JoinColumn,
  Index,
} from 'typeorm';
import type { PlatformType } from '@media-manager/shared';
import { User } from './User.js';
import { PlatformAccount } from './PlatformAccount.js';
import { Work } from './Work.js';

@Entity('comments')
@Index(['userId', 'workId'])
export class Comment {
  @PrimaryGeneratedColumn()
  id!: number;

  @Column({ type: 'int', name: 'user_id' })
  userId!: number;

  @Column({ type: 'int', name: 'account_id' })
  accountId!: number;

  @Column({ type: 'int', name: 'work_id', nullable: true })
  workId!: number | null;

  @Column({ type: 'varchar', length: 50, nullable: true })
  platform!: PlatformType | null;

  @Column({ type: 'varchar', length: 100, nullable: true, name: 'video_id' })
  videoId!: string | null;

  @Column({ type: 'varchar', length: 500, nullable: true, name: 'platform_video_url' })
  platformVideoUrl!: string | null;

  @Column({ type: 'varchar', length: 100, unique: true, name: 'comment_id' })
  commentId!: string;

  @Column({ type: 'varchar', length: 100, nullable: true, name: 'parent_comment_id' })
  parentCommentId!: string | null;

  @Column({ type: 'varchar', length: 100, nullable: true, name: 'author_id' })
  authorId!: string | null;

  @Column({ type: 'varchar', length: 100, nullable: true, name: 'author_name' })
  authorName!: string | null;

  @Column({ type: 'varchar', length: 500, nullable: true, name: 'author_avatar' })
  authorAvatar!: string | null;

  @Column({ type: 'text', nullable: true })
  content!: string | null;

  @Column({ type: 'int', default: 0, name: 'like_count' })
  likeCount!: number;

  @Column({ type: 'text', nullable: true, name: 'reply_content' })
  replyContent!: string | null;

  @Column({ type: 'timestamp', nullable: true, name: 'replied_at' })
  repliedAt!: Date | null;

  @Column({ type: 'boolean', default: false, name: 'is_read' })
  isRead!: boolean;

  @Column({ type: 'boolean', default: false, name: 'is_top' })
  isTop!: boolean;

  @Column({ type: 'timestamp', nullable: true, name: 'comment_time' })
  commentTime!: Date | null;

  @CreateDateColumn({ name: 'created_at' })
  createdAt!: Date;

  // 关联
  @ManyToOne(() => User, (user) => user.comments, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'user_id' })
  user!: User;

  @ManyToOne(() => PlatformAccount, (account) => account.comments, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'account_id' })
  account!: PlatformAccount;

  @ManyToOne(() => Work, { onDelete: 'SET NULL', nullable: true })
  @JoinColumn({ name: 'work_id' })
  work?: Work;
}
