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

export class ArchiveMetadata extends Model<InferAttributes<ArchiveMetadata>, InferCreationAttributes<ArchiveMetadata>> {
  declare id: CreationOptional<string>;
  declare archive_file: string;
  declare created_at: CreationOptional<Date>;
  declare updated_at: CreationOptional<Date>;

  declare date_range_start: number;
  declare date_range_end: number;

  declare tables: Record<
    string,
    {
      archived_count: number;
      deleted_count: number;
      failed_count: number;
      failed_ids?: string[];
    }
  >;
  declare total_records: number;

  declare checksum?: string;
  declare file_size?: number;

  declare duration_ms?: number;
  declare triggered_by: 'cron' | 'manual';
  declare triggered_by_user_id?: string;
  declare status: 'completed' | 'failed' | 'in_progress';
  declare error?: string;

  declare query_count: number;
  declare query_actor_ids?: string[];
  declare last_queried_at?: number;

  public static readonly GENESIS_ATTRIBUTES = {
    id: {
      type: DataTypes.STRING(40),
      primaryKey: true,
      allowNull: false,
    },
    archive_file: {
      type: DataTypes.STRING(128),
      allowNull: false,
    },
    date_range_start: {
      type: DataTypes.INTEGER,
      allowNull: false,
    },
    date_range_end: {
      type: DataTypes.INTEGER,
      allowNull: false,
    },
    tables: {
      type: DataTypes.JSON,
      allowNull: false,
      defaultValue: {},
    },
    total_records: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
    },
    checksum: {
      type: DataTypes.STRING(128),
      allowNull: true,
    },
    file_size: {
      type: DataTypes.INTEGER,
      allowNull: true,
    },
    duration_ms: {
      type: DataTypes.INTEGER,
      allowNull: true,
    },
    triggered_by: {
      type: DataTypes.ENUM('cron', 'manual'),
      allowNull: false,
    },
    triggered_by_user_id: {
      type: DataTypes.STRING(64),
      allowNull: true,
    },
    status: {
      type: DataTypes.ENUM('completed', 'failed', 'in_progress'),
      allowNull: false,
    },
    error: {
      type: DataTypes.TEXT,
      allowNull: true,
    },
    query_count: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
    },
    query_actor_ids: {
      type: DataTypes.JSON,
      allowNull: true,
      defaultValue: [],
    },
    last_queried_at: {
      type: DataTypes.INTEGER,
      allowNull: true,
    },
    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(ArchiveMetadata.GENESIS_ATTRIBUTES, {
      sequelize,
      modelName: 'ArchiveMetadata',
      tableName: 'archive_metadata',
      createdAt: 'created_at',
      updatedAt: 'updated_at',
    });
  }

  public static associate() {}
}

export type TArchiveMetadata = InferAttributes<ArchiveMetadata>;
