import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, Index, Unique } from 'typeorm';

// 定义 Permission 类型
export type Permission = string;

@Entity({ name: 'role' })
@Unique('uk_tenant_name', ['tenantId', 'name'])
export class Role {
  @PrimaryGeneratedColumn({ unsigned: true })
  id!: number;

  @Column({
    name: 'tenant_id',
    type: 'int',
    unsigned: true,
    nullable: false,
    comment: '租户ID'
  })
  @Index('idx_tenant_id')
  tenantId!: number;

  @Column({ type: 'varchar', length: 100 })
  name!: string;

  @Column({ type: 'varchar', length: 500, nullable: true })
  description!: string | null;

  @Column({ type: 'simple-array', nullable: false })
  permissions: Permission[] = [];

  @CreateDateColumn({ name: 'created_at', type: 'timestamp' })
  createdAt!: Date;

  @UpdateDateColumn({ name: 'updated_at', type: 'timestamp' })
  updatedAt!: Date;

  constructor(partial?: Partial<Role>) {
    Object.assign(this, partial);
    if (!this.permissions) {
      this.permissions = [];
    }
  }
}