import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn, CreateDateColumn, UpdateDateColumn, OneToMany } from 'typeorm';
import { User } from '@/server/modules/users/user.entity';
import { File } from '@/server/modules/files/file.entity';
import { SolutionChapter } from './solution-chapter.entity';

@Entity('solution_designs')
export class SolutionDesign {
  @PrimaryGeneratedColumn({ unsigned: true, comment: '方案设计ID，主键自增' })
  id!: number;

  @Column({ name: 'title', type: 'varchar', length: 255, comment: '方案标题' })
  title!: string;

  @Column({ name: 'description', type: 'text', nullable: true, comment: '方案描述' })
  description!: string | null;

  @Column({ name: 'requirements', type: 'text', nullable: true, comment: '需求描述' })
  requirements!: string | null;

  @Column({ name: 'user_id', type: 'int', unsigned: true, comment: '创建用户ID' })
  userId!: number;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'user_id', referencedColumnName: 'id' })
  user!: User;

  @Column({ name: 'original_file_id', type: 'int', unsigned: true, nullable: true, comment: '原始文件ID' })
  originalFileId!: number | null;

  @ManyToOne(() => File)
  @JoinColumn({ name: 'original_file_id', referencedColumnName: 'id' })
  originalFile!: File | null;

  @Column({ name: 'output_file_id', type: 'int', unsigned: true, nullable: true, comment: '输出文件ID' })
  outputFileId!: number | null;

  @ManyToOne(() => File)
  @JoinColumn({ name: 'output_file_id', referencedColumnName: 'id' })
  outputFile!: File | null;

  @Column({ name: 'output_format', type: 'varchar', length: 10, default: 'docx', comment: '输出格式：docx/pdf' })
  outputFormat!: string;

  @Column({ name: 'status', type: 'varchar', length: 20, default: 'draft', comment: '状态：draft-草稿, reviewing-审核中, completed-已完成' })
  status!: string;

  @Column({ name: 'progress', type: 'int', default: 0, comment: '完成进度百分比' })
  progress!: number;

  @Column({ name: 'total_chapters', type: 'int', default: 0, comment: '总章节数' })
  totalChapters!: number;

  @Column({ name: 'completed_chapters', type: 'int', default: 0, comment: '已完成章节数' })
  completedChapters!: number;

  @Column({ name: 'is_deleted', type: 'tinyint', default: 0, comment: '是否删除：0-未删除，1-已删除' })
  isDeleted!: number;

  @CreateDateColumn({ name: 'created_at', comment: '创建时间' })
  createdAt!: Date;

  @UpdateDateColumn({ name: 'updated_at', comment: '更新时间' })
  updatedAt!: Date;

  @OneToMany(() => SolutionChapter, chapter => chapter.solutionDesign)
  chapters!: SolutionChapter[];
}