/**
 * License ORM Entity (Infrastructure Layer)
 *
 * TypeORM entity for license persistence.
 * Represents user licenses linked to subscriptions.
 */

import { Entity, Column, PrimaryGeneratedColumn, ManyToOne, JoinColumn, Index } from 'typeorm';
import { SubscriptionOrmEntity } from './subscription.orm-entity';
import { UserOrmEntity } from './user.orm-entity';

export type LicenseStatusOrmType = 'ACTIVE' | 'REVOKED';

@Entity('licenses')
@Index('idx_licenses_subscription_id', ['subscriptionId'])
@Index('idx_licenses_user_id', ['userId'])
@Index('idx_licenses_status', ['status'])
@Index('idx_licenses_subscription_status', ['subscriptionId', 'status'])
export class LicenseOrmEntity {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ name: 'subscription_id', type: 'uuid' })
  subscriptionId: string;

  @ManyToOne(() => SubscriptionOrmEntity, subscription => subscription.licenses, {
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'subscription_id' })
  subscription: SubscriptionOrmEntity;

  @Column({ name: 'user_id', type: 'uuid', unique: true })
  userId: string;

  @ManyToOne(() => UserOrmEntity, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'user_id' })
  user: UserOrmEntity;

  // Status
  @Column({
    type: 'enum',
    enum: ['ACTIVE', 'REVOKED'],
    default: 'ACTIVE',
  })
  status: LicenseStatusOrmType;

  // Timestamps
  @Column({ name: 'assigned_at', type: 'timestamp', default: () => 'NOW()' })
  assignedAt: Date;

  @Column({ name: 'revoked_at', type: 'timestamp', nullable: true })
  revokedAt: Date | null;
}
