import { FunctionPermission } from '@/modules/authorization/decorators/has-feature.decorator';
import { Expose } from '@nestjs/class-transformer';
import { IsBoolean, IsString } from 'class-validator';
import { Column, Entity, ManyToMany, OneToMany, Unique } from 'typeorm';
import { CoreEntity } from './core/core-entity';
import { ResourcePermission } from './resource-permission.entity';
import { Role } from './role.entity';

@Entity('resources')
@Unique(['name', 'group_key'])
@Unique(['path', 'method'])
export class Resource extends CoreEntity {
  // Tên method bên trong controller
  @Column({ type: 'varchar', nullable: true })
  method_name: string;

  // Mô tả tính năng
  @IsString()
  @Column({ type: 'varchar', nullable: true })
  description: string;

  // Nhóm tính năng
  @IsString()
  @Column({ type: 'varchar', nullable: true })
  name: string;

  // Key nhóm (để mapping)
  @Column({ type: 'varchar', nullable: true })
  group_key: string;

  @Column({ type: 'varchar', nullable: true, default: null })
  path: string;

  @Column({ type: 'varchar', nullable: true, default: null })
  method: string;

  // Loại định nghĩa (controller/method)
  @Column({ type: 'varchar' })
  type: 'controller' | 'method';

  // Có được public hay cần auth
  @IsBoolean()
  @Column({ type: 'boolean', default: false })
  is_public: boolean;

  // Có bị disable (ẩn tính năng này)
  @IsBoolean()
  @Column({ type: 'boolean', default: false })
  disabled: boolean;

  @Column({
    type: 'enum',
    default: null,
    enum: FunctionPermission,
    nullable: true,
  })
  function_permisstion: FunctionPermission;

  @OneToMany(() => ResourcePermission, (rp) => rp.resource, { cascade: true })
  resource_permissions: ResourcePermission[];

  @ManyToMany(() => Role, (role) => role.resources)
  roles: Role[];

  @Expose()
  children: Resource[] = [];

  @Expose()
  checked?: boolean;
}
