/**
 * @aurora-catalyst-generated
 * @source cliter/iam/tenant.aurora.yaml
 */
import { IamAccountModel } from '@app/iam/account';
import { IamTenantAccountModel } from '@app/iam/tenant-account';
import { Auditable } from '@aurorajs.dev/core-back';
import { DataTypes } from 'sequelize';
import { BelongsTo, BelongsToMany, Column, ForeignKey, Model, Table } from 'sequelize-typescript';

@Auditable()
@Table({
  modelName: 'IamTenant',
  tableName: 'iam_tenant',
  freezeTableName: true,
  timestamps: false,
  indexes: [
    {
      fields: ['row_id'],
      unique: true,
    },
    {
      fields: ['code'],
      unique: true,
    },
  ],
})
export class IamTenantModel extends Model<IamTenantModel> {
  @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;

  @ForeignKey(() => IamTenantModel)
  @Column({
    field: 'parent_id',
    allowNull: true,
    type: DataTypes.UUID,
  })
  parentId: string;

  @BelongsTo(() => IamTenantModel, {
    constraints: false,
    foreignKey: 'parent_id',
  })
  parent: IamTenantModel;

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

  @Column({
    field: 'code',
    allowNull: true,
    type: DataTypes.STRING(64),
  })
  code: string;

  @Column({
    field: 'logo',
    allowNull: true,
    type: DataTypes.JSONB,
  })
  logo: any;

  @Column({
    field: 'is_active',
    allowNull: false,
    type: DataTypes.BOOLEAN,
    defaultValue: false,
  })
  isActive: boolean;

  @Column({
    field: 'meta',
    allowNull: true,
    type: DataTypes.JSONB,
  })
  meta: any;

  @BelongsToMany(() => IamAccountModel, {
    through: () => IamTenantAccountModel,
    uniqueKey: 'Uq01IamTenantAccount',
  })
  accounts: IamAccountModel[];

  @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;
}
