/**
 * @aurora-catalyst-generated
 * @source cliter/o-auth/credential.aurora.yaml
 */
import { IamAccountModel } from '@app/iam/account';
import { OAuthAccessTokenModel } from '@app/o-auth/access-token';
import { OAuthRefreshTokenModel } from '@app/o-auth/refresh-token';
import { DataTypes } from 'sequelize';
import { BelongsTo, Column, ForeignKey, Model, Table } from 'sequelize-typescript';

@Table({
  modelName: 'OAuthCredential',
  tableName: 'o_auth_credential',
  freezeTableName: true,
  timestamps: false,
  indexes: [
    {
      fields: ['row_id'],
      unique: true,
    },
    {
      fields: ['grant_type'],
      unique: false,
    },
    {
      fields: ['account_id'],
      unique: false,
    },
    {
      fields: ['ip_address'],
      unique: false,
    },
    {
      fields: ['created_at'],
      unique: false,
    },
  ],
})
export class OAuthCredentialModel extends Model<OAuthCredentialModel> {
  @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('PASSWORD', 'REFRESH_TOKEN', 'AUTHORIZATION_CODE'),
  })
  grantType: string;

  @ForeignKey(() => IamAccountModel)
  @Column({
    field: 'account_id',
    allowNull: false,
    type: DataTypes.UUID,
  })
  accountId: string;

  @BelongsTo(() => IamAccountModel, {
    constraints: false,
    foreignKey: 'account_id',
  })
  account: IamAccountModel;

  @ForeignKey(() => OAuthAccessTokenModel)
  @Column({
    field: 'access_token_id',
    allowNull: true,
    type: DataTypes.UUID,
  })
  accessTokenId: string;

  @BelongsTo(() => OAuthAccessTokenModel, {
    constraints: false,
    foreignKey: 'access_token_id',
  })
  accessToken: OAuthAccessTokenModel;

  @ForeignKey(() => OAuthRefreshTokenModel)
  @Column({
    field: 'refresh_token_id',
    allowNull: true,
    type: DataTypes.UUID,
  })
  refreshTokenId: string;

  @BelongsTo(() => OAuthRefreshTokenModel, {
    constraints: false,
    foreignKey: 'refresh_token_id',
  })
  refreshToken: OAuthRefreshTokenModel;

  @Column({
    field: 'ip_address',
    allowNull: true,
    type: DataTypes.STRING(45),
  })
  ipAddress: string;

  @Column({
    field: 'user_agent',
    allowNull: true,
    type: DataTypes.STRING(255),
  })
  userAgent: string;

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