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

@Entity('roles')
export class Role {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({ type: 'varchar', length: 50 })
  role_name: string;

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

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

  @Column({ type: 'jsonb', default: {} })
  permissions: Record<string, any>;

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

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

  @ManyToOne(() => Role, { onDelete: 'SET NULL', nullable: true })
  @JoinColumn({ name: 'is_cloned_from' })
  cloned_from: Role | null;
}
