import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn, Unique, Check } from 'typeorm';
import { License } from './license.entity';
import { Module } from './module.entity';

@Entity('module_installations')
@Unique(['license_id', 'module_id'])
@Check(`"status" IN ('pending', 'installing', 'installed', 'failed')`)
export class ModuleInstallation {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ type: 'uuid' })
  license_id: string;

  @Column({ type: 'uuid' })
  module_id: string;

  @Column({ type: 'varchar', length: 50, default: 'pending' })
  status: string;

  @Column({ type: 'timestamptz', nullable: true })
  installed_at: Date | null;

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

  @ManyToOne(() => License, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'license_id' })
  license: License;

  @ManyToOne(() => Module, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'module_id' })
  module: Module;
}
