const { DataTypes } = require("sequelize");

/**
 * Model: workactivityitemfiles (multi-dokumen per item).
 * Pattern: lowercase, no underscores, plural.
 * Satu baris = satu file bukti per workactivityitem (workactivityitems.id).
 */
module.exports = (sequelize) => {
  const WorkActivityItemFile = sequelize.define(
    "workactivityitemfile",
    {
      id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true,
      },
      creation: { type: DataTypes.DATE, allowNull: false },
      modified: { type: DataTypes.DATE, allowNull: false },
      modified_by: { type: DataTypes.INTEGER, allowNull: true },
      owner: { type: DataTypes.INTEGER, allowNull: true },
      enabled: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: true },

      workactivityitem_id: { type: DataTypes.INTEGER, allowNull: false },
      file_name: { type: DataTypes.STRING(255), allowNull: true },
      file_path: { type: DataTypes.STRING(500), allowNull: false },
      sort_order: { type: DataTypes.INTEGER, allowNull: true, defaultValue: 0 },
    },
    {
      tableName: "workactivityitemfiles",
      freezeTableName: true,
      indexes: [
        { name: "idx_workactivityitemfiles_item_id", fields: ["workactivityitem_id"] },
      ],
    }
  );

  WorkActivityItemFile.associate = (models) => {
    WorkActivityItemFile.belongsTo(models.WorkActivityItem, {
      foreignKey: "workactivityitem_id",
      as: "workactivityitem",
      onDelete: "CASCADE",
    });
  };

  return WorkActivityItemFile;
};
