/**
 * @aurora-catalyst-generated
 * @source cliter/o-auth/client.aurora.yaml
 */
import { OAuthAccessTokenModel } from '@app/o-auth/access-token';
import { OAuthApplicationModel } from '@app/o-auth/application';
import { OAuthApplicationClientModel } from '@app/o-auth/application-client';
import { Auditable } from '@aurorajs.dev/core-back';
import { DataTypes } from 'sequelize';
import { BelongsToMany, Column, HasMany, Model, Table } from 'sequelize-typescript';

@Auditable()
@Table({
  modelName: 'OAuthClient',
  tableName: 'o_auth_client',
  freezeTableName: true,
  timestamps: false,
  indexes: [
    {
      fields: ['row_id'],
      unique: true,
    },
  ],
})
export class OAuthClientModel extends Model<OAuthClientModel> {
  @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: 'grant_type',
    allowNull: false,
    type: DataTypes.ENUM('AUTHORIZATION_CODE', 'CLIENT_CREDENTIALS', 'PASSWORD', 'REFRESH_TOKEN'),
  })
  grantType: string;

  @BelongsToMany(() => OAuthApplicationModel, {
    through: () => OAuthApplicationClientModel,
    uniqueKey: 'Uq01OAuthApplicationClient',
    constraints: false,
  })
  applications: OAuthApplicationModel[];

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

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

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

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

  @Column({
    field: 'auth_url',
    allowNull: true,
    type: DataTypes.STRING(2046),
  })
  authUrl: string;

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

  @Column({
    field: 'access_token_lifetime_seconds',
    allowNull: true,
    type: DataTypes.INTEGER,
  })
  accessTokenLifetimeSeconds: number;

  @Column({
    field: 'refresh_token_lifetime_seconds',
    allowNull: true,
    type: DataTypes.INTEGER,
  })
  refreshTokenLifetimeSeconds: number;

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

  @HasMany(() => OAuthAccessTokenModel, {
    constraints: false,
  })
  accessTokens: OAuthAccessTokenModel[];

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