import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  ManyToOne,
  JoinColumn,
  Index,
} from 'typeorm';
import { LegalPage } from './legal-page.entity';
import { User } from './user.entity';

@Entity('legal_page_versions')
@Index(['legalPageId', 'version'], { unique: true })
export class LegalPageVersion {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ name: 'legal_page_id', type: 'uuid' })
  @Index()
  legalPageId: string;

  @ManyToOne(() => LegalPage, legalPage => legalPage.versions, {
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'legal_page_id' })
  legalPage: LegalPage;

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

  @Column({ type: 'varchar', length: 255 })
  title: string;

  @Column({ type: 'text' })
  content: string;

  @Column({ name: 'created_by', type: 'varchar', length: 20, nullable: true })
  createdBy: string;

  @ManyToOne(() => User, { nullable: true })
  @JoinColumn({ name: 'created_by' })
  createdByUser: User;

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