/**
 * @aurora-catalyst-generated
 * @source cliter/iam/permission.aurora.yaml
 */
import { IamBoundedContextModel } from '@app/iam/bounded-context';
import { IamPermissionRoleModel } from '@app/iam/permission-role';
import { IamRoleModel } from '@app/iam/role';
import { Auditable } from '@aurorajs.dev/core-back';
import { DataTypes } from 'sequelize';
import { BelongsTo, BelongsToMany, Column, ForeignKey, Model, Table } from 'sequelize-typescript';

@Auditable()
@Table({
  modelName: 'IamPermission',
  tableName: 'iam_permission',
  freezeTableName: true,
  timestamps: false,
  indexes: [
    {
      fields: ['row_id'],
      unique: true,
    },
    {
      fields: ['bounded_context_id'],
      unique: false,
    },
  ],
})
export class IamPermissionModel extends Model<IamPermissionModel> {
  @Column({
    field: 'id',
    primaryKey: true,
    allowNull: false,
    type: DataTypes.UUID,
  })
  id: string;

  @Column({
    field: 'row_id',
    autoIncrement: true,
    allowNull: false,
    type: DataTypes.BIGINT,
  })
  rowId: number;

  @Column({
    field: 'name',
    allowNull: false,
    type: DataTypes.STRING(128),
  })
  name: string;

  @ForeignKey(() => IamBoundedContextModel)
  @Column({
    field: 'bounded_context_id',
    allowNull: false,
    type: DataTypes.UUID,
  })
  boundedContextId: string;

  @BelongsTo(() => IamBoundedContextModel, {
    constraints: false,
    foreignKey: 'bounded_context_id',
  })
  boundedContext: IamBoundedContextModel;

  @BelongsToMany(() => IamRoleModel, {
    through: () => IamPermissionRoleModel,
    uniqueKey: 'Uq01IamPermissionRole',
  })
  roles: IamRoleModel[];

  @Column({
    field: 'created_at',
    allowNull: true,
    type: DataTypes.DATE,
  })
  createdAt: string;

  @Column({
    field: 'updated_at',
    allowNull: true,
    type: DataTypes.DATE,
  })
  updatedAt: string;

  @Column({
    field: 'deleted_at',
    allowNull: true,
    type: DataTypes.DATE,
  })
  deletedAt: string;
}
