import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn, Index } from 'typeorm';
import { PlatformAccount } from './PlatformAccount.js';

@Entity('works')
@Index(['userId', 'platform'])
@Index(['accountId', 'platformVideoId'], { unique: true })
export class Work {
  @PrimaryGeneratedColumn()
  id!: number;

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

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

  @Column({ type: 'varchar', length: 20 })
  platform!: string;

  /** 平台作品 ID，视频号 objectId 较长，需 500 字符 */
  @Column({ name: 'platform_video_id', type: 'varchar', length: 500 })
  platformVideoId!: string;

  @Column({ type: 'varchar', length: 200, default: '' })
  title!: string;

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

  @Column({ name: 'cover_url', type: 'varchar', length: 500, default: '' })
  coverUrl!: string;

  // 抖音 play_addr.url_list[0] 等直链可能非常长，使用 TEXT 避免长度不足导致同步失败
  @Column({ name: 'video_url', type: 'text', nullable: true })
  videoUrl!: string | null;

  @Column({ type: 'varchar', length: 20, default: '00:00' })
  duration!: string;

  @Column({ type: 'varchar', length: 20, default: 'published' })
  status!: string;

  @Column({ name: 'publish_time', type: 'datetime', nullable: true })
  publishTime!: Date | null;

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

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

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

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

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

  // ===== 昨日数据快照（yesterday_*）=====

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

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

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

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

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

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

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

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

  @Column({ name: 'yesterday_cover_click_rate', type: 'varchar', length: 50, default: '0' })
  yesterdayCoverClickRate!: string;

  @Column({ name: 'yesterday_avg_watch_duration', type: 'varchar', length: 50, default: '0' })
  yesterdayAvgWatchDuration!: string;

  @Column({ name: 'yesterday_total_watch_duration', type: 'varchar', length: 50, default: '0' })
  yesterdayTotalWatchDuration!: string;

  @Column({ name: 'yesterday_completion_rate', type: 'varchar', length: 50, default: '0' })
  yesterdayCompletionRate!: string;

  @Column({ name: 'yesterday_two_second_exit_rate', type: 'varchar', length: 50, default: '0' })
  yesterdayTwoSecondExitRate!: string;

  @Column({ name: 'yesterday_completion_rate_5s', type: 'varchar', length: 50, default: '0' })
  yesterdayCompletionRate5s!: string;

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

  @Column({ type: 'datetime', name: 'created_at', default: () => 'CURRENT_TIMESTAMP' })
  createdAt!: Date;

  @Column({
    type: 'datetime',
    name: 'updated_at',
    default: () => 'CURRENT_TIMESTAMP',
    onUpdate: 'CURRENT_TIMESTAMP',
  })
  updatedAt!: Date;

  @ManyToOne(() => PlatformAccount, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'accountId' })
  account?: PlatformAccount;
}
