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

@Entity('legal_pages')
@Index(['slug', 'language'], { unique: true })
export class LegalPage {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ type: 'varchar', length: 100 })
  @Index()
  slug: string;

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

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

  @Column({ type: 'varchar', length: 5, default: 'en' })
  @Index()
  language: string;

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

  @Column({ name: 'is_published', type: 'boolean', default: false })
  @Index()
  isPublished: boolean;

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

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

  @OneToMany(() => LegalPageVersion, version => version.legalPage, {
    cascade: true,
  })
  versions: LegalPageVersion[];

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

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