/**
 * Form 16 – 26AS upload audit log.
 * Records each 26AS file upload: who uploaded, when, and how many records were imported.
 */

import { QueryInterface, DataTypes } from 'sequelize';

export async function up(queryInterface: QueryInterface): Promise<void> {
  await queryInterface.createTable('form_16_26as_upload_log', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    uploaded_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
    uploaded_by: {
      type: DataTypes.UUID,
      allowNull: false,
      references: { model: 'users', key: 'user_id' },
    },
    file_name: {
      type: DataTypes.STRING(255),
      allowNull: true,
    },
    records_imported: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
    },
    errors_count: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
    },
  });

  await queryInterface.addIndex('form_16_26as_upload_log', ['uploaded_at'], {
    name: 'idx_form_16_26as_upload_log_uploaded_at',
  });
  await queryInterface.addIndex('form_16_26as_upload_log', ['uploaded_by'], {
    name: 'idx_form_16_26as_upload_log_uploaded_by',
  });
}

export async function down(queryInterface: QueryInterface): Promise<void> {
  await queryInterface.dropTable('form_16_26as_upload_log');
}
