import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn, Check } from 'typeorm';
import { User } from './user.entity';

@Entity('licenses')
@Check(`"status" IN ('active', 'suspended', 'expired', 'revoked')`)
export class License {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ type: 'varchar', length: 64, unique: true })
  license_key: string;

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

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

  @Column({ type: 'varchar', length: 100, nullable: true })
  server_name: string | null;

  @Column({ type: 'varchar', length: 63, unique: true, nullable: true })
  subdomain: string | null;

  @Column({ type: 'varchar', length: 255, unique: true, nullable: true })
  custom_domain: string | null;

  @Column('text', { array: true, default: '{}' })
  modules_enabled: string[];

  @Column({ type: 'boolean', default: false })
  webstore_active: boolean;

  @Column({ type: 'varchar', length: 100, nullable: true })
  webstore_subscription_id: string | null;

  @Column({ type: 'timestamptz', default: () => 'NOW()' })
  created_at: Date;

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

  @ManyToOne(() => User, user => user.licenses, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'owner_user_id' })
  owner: User;
}
