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

@Entity('publish_results')
export class PublishResult {
  @PrimaryGeneratedColumn()
  id!: number;

  @Column({ type: 'int', name: 'task_id' })
  taskId!: number;

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

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

  @Column({
    type: 'enum',
    enum: ['success', 'failed'],
    nullable: true,
  })
  status!: PublishResultStatus | null;

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

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

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

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

  // 关联
  @ManyToOne(() => PublishTask, (task) => task.results, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'task_id' })
  task!: PublishTask;

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