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

@Table({
  modelName: 'OAuthAccessToken',
  tableName: 'o_auth_access_token',
  freezeTableName: true,
  timestamps: false,
  indexes: [
    {
      fields: ['row_id'],
      unique: true,
    },
    {
      fields: ['client_id'],
      unique: false,
    },
  ],
})
export class OAuthAccessTokenModel extends Model<OAuthAccessTokenModel> {
  @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(() => OAuthClientModel)
  @Column({
    field: 'client_id',
    allowNull: false,
    type: DataTypes.UUID,
  })
  clientId: string;

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

  @Column({
    field: 'account_id',
    allowNull: true,
    type: DataTypes.UUID,
  })
  accountId: string;

  @Column({
    field: 'token',
    allowNull: false,
    type: DataTypes.TEXT,
  })
  token: string;

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

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

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

  @HasOne(() => OAuthRefreshTokenModel, {
    constraints: false,
  })
  refreshToken: OAuthRefreshTokenModel;

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