/**
 * @aurora-catalyst-generated
 * @source cliter/iam/account.aurora.yaml
 */
import { IamRoleModel } from '@app/iam/role';
import { IamRoleAccountModel } from '@app/iam/role-account';
import { IamTenantModel } from '@app/iam/tenant';
import { IamTenantAccountModel } from '@app/iam/tenant-account';
import { IamUserModel } from '@app/iam/user';
import { OAuthClientModel } from '@app/o-auth/client';
import { Auditable } from '@aurorajs.dev/core-back';
import { DataTypes } from 'sequelize';
import {
  BelongsTo,
  BelongsToMany,
  Column,
  ForeignKey,
  HasOne,
  Model,
  Table,
} from 'sequelize-typescript';

@Auditable()
@Table({
  modelName: 'IamAccount',
  tableName: 'iam_account',
  freezeTableName: true,
  timestamps: false,
  indexes: [
    {
      fields: ['row_id'],
      unique: true,
    },
    {
      fields: ['code'],
      unique: true,
    },
    {
      fields: ['email'],
      unique: true,
    },
    {
      fields: ['username'],
      unique: true,
    },
    {
      fields: ['tags'],
      unique: false,
      using: 'GIN',
    },
  ],
})
export class IamAccountModel extends Model<IamAccountModel> {
  @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: 'type',
    allowNull: false,
    type: DataTypes.ENUM('USER', 'SERVICE'),
  })
  type: string;

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

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

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

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

  @ForeignKey(() => OAuthClientModel)
  @Column({
    field: 'client_id',
    allowNull: false,
    type: DataTypes.UUID,
  })
  clientId: string;

  @BelongsTo(() => OAuthClientModel, {
    constraints: false,
    foreignKey: 'client_id',
  })
  client: OAuthClientModel;

  @Column({
    field: 'tags',
    allowNull: true,
    type: DataTypes.ARRAY(DataTypes.STRING()),
  })
  tags: string[];

  @Column({
    field: 'scopes',
    allowNull: true,
    type: DataTypes.ARRAY(DataTypes.STRING()),
  })
  scopes: string[];

  @Column({
    field: 'd_application_codes',
    allowNull: false,
    type: DataTypes.ARRAY(DataTypes.STRING()),
  })
  dApplicationCodes: string[];

  @Column({
    field: 'd_permissions',
    allowNull: false,
    type: DataTypes.JSONB,
  })
  dPermissions: any;

  @Column({
    field: 'd_tenants',
    allowNull: false,
    type: DataTypes.ARRAY(DataTypes.UUID),
  })
  dTenants: string[];

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

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

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

  @HasOne(() => IamUserModel)
  user: IamUserModel;

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