/* eslint-disable @typescript-eslint/lines-between-class-members */
import { CreationOptional, DataTypes, InferAttributes, InferCreationAttributes, Model } from 'sequelize';

// eslint-disable-next-line prettier/prettier
export class Job extends Model<InferAttributes<Job>, InferCreationAttributes<Job>> {
  declare id: CreationOptional<string>;

  declare queue: string;
  declare job: any;
  declare retry_count: number;
  declare delay?: number;
  declare will_run_at?: number;
  declare cancelled?: boolean;

  declare created_at: CreationOptional<Date>;
  declare updated_at: CreationOptional<Date>;

  public static readonly GENESIS_ATTRIBUTES = {
    id: {
      type: DataTypes.STRING(30),
      primaryKey: true,
      allowNull: false,
    },
    queue: {
      type: DataTypes.STRING(32),
      allowNull: false,
    },
    job: {
      type: DataTypes.JSON,
      allowNull: false,
    },
    retry_count: {
      type: DataTypes.INTEGER,
      defaultValue: 0,
    },
    delay: {
      type: DataTypes.INTEGER,
      defaultValue: -1,
    },
    will_run_at: {
      type: DataTypes.INTEGER,
      defaultValue: -1,
    },
    cancelled: {
      type: DataTypes.BOOLEAN,
      defaultValue: false,
    },
    created_at: {
      type: DataTypes.DATE,
      defaultValue: DataTypes.NOW,
      allowNull: false,
    },
    updated_at: {
      type: DataTypes.DATE,
      defaultValue: DataTypes.NOW,
      allowNull: false,
    },
  };

  public static initialize(sequelize: any) {
    this.init(Job.GENESIS_ATTRIBUTES, {
      sequelize,
      modelName: 'Job',
      tableName: 'jobs',
      createdAt: 'created_at',
      updatedAt: 'updated_at',
    });
  }

  public static associate() {
    // Do nothing
  }

  public static isInitialized(): boolean {
    return this.sequelize !== undefined;
  }
}

export type TJob = InferAttributes<Job>;
