import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  OneToMany,
  JoinColumn,
} from 'typeorm';
import type { PublishTaskStatus, PlatformPublishConfig } from '@media-manager/shared';
import { User } from './User.js';
import { PublishResult } from './PublishResult.js';

@Entity('publish_tasks')
export class PublishTask {
  @PrimaryGeneratedColumn()
  id!: number;

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

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

  @Column({ type: 'varchar', length: 255, nullable: true, name: 'video_filename' })
  videoFilename!: string | null;

  @Column({ type: 'varchar', length: 200, nullable: true })
  title!: string | null;

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

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

  @Column({ type: 'json', nullable: true })
  tags!: string[] | null;

  @Column({ type: 'json', nullable: true, name: 'target_accounts' })
  targetAccounts!: number[] | null;

  @Column({ type: 'json', nullable: true, name: 'platform_configs' })
  platformConfigs!: PlatformPublishConfig[] | null;

  @Column({
    type: 'enum',
    enum: ['pending', 'processing', 'completed', 'failed', 'cancelled'],
    default: 'pending',
  })
  status!: PublishTaskStatus;

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

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

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

  @UpdateDateColumn({ name: 'updated_at' })
  updatedAt!: Date;

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

  @OneToMany(() => PublishResult, (result) => result.task)
  results!: PublishResult[];
}
