module.exports = (sequelize, DataTypes) => {
  const MachineHistoryCard = sequelize.define('MachineHistoryCard', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      },
      comment: 'Company this history card belongs to'
    },
    plant_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Plant',
        key: 'id'
      },
      comment: 'Plant where the machine is located'
    },
    fiscal_year_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'FiscalYear',
        key: 'id'
      },
      comment: 'Fiscal year for this history card'
    },
    machine_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Machine',
        key: 'id'
      },
      comment: 'Machine this history card is for'
    },
    machine_number: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'Machine number/code'
    },
    machine_description: {
      type: DataTypes.STRING(200),
      allowNull: true,
      comment: 'Machine description/name'
    },
    date: {
      type: DataTypes.DATEONLY,
      allowNull: false,
      comment: 'Date of the problem/maintenance'
    },
    problem_details: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'Details of the problem'
    },
    idle_time_from: {
      type: DataTypes.TIME,
      allowNull: true,
      comment: 'Idle time start'
    },
    idle_time_to: {
      type: DataTypes.TIME,
      allowNull: true,
      comment: 'Idle time end'
    },
    total_idle_hours: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: true,
      comment: 'Total idle hours calculated'
    },
    action_taken: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'Action taken to resolve the problem'
    },
    spares_replaced: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'Details of spares replaced if any'
    },
    cost_if_known: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: true,
      comment: 'Cost of repair/maintenance if known'
    },
    signed_by: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: 'Person who signed/approved the card'
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'Additional remarks'
    },
    status: {
      type: DataTypes.ENUM('OPEN', 'IN_PROGRESS', 'RESOLVED', 'CLOSED'),
      allowNull: false,
      defaultValue: 'OPEN',
      comment: 'Status of the history card'
    },
    created_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    updated_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    }
  }, {
    tableName: 'machine_history_card',
    underscored: true,
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    indexes: [
      {
        fields: ['company_id']
      },
      {
        fields: ['plant_id']
      },
      {
        fields: ['machine_id']
      },
      {
        fields: ['date']
      },
      {
        fields: ['status']
      }
    ]
  });

  MachineHistoryCard.associate = function(models) {
    MachineHistoryCard.belongsTo(models.Company, {
      foreignKey: 'company_id',
      as: 'company'
    });

    MachineHistoryCard.belongsTo(models.Plant, {
      foreignKey: 'plant_id',
      as: 'plant'
    });

    MachineHistoryCard.belongsTo(models.FiscalYear, {
      foreignKey: 'fiscal_year_id',
      as: 'fiscalYear'
    });

    MachineHistoryCard.belongsTo(models.Machine, {
      foreignKey: 'machine_id',
      as: 'machine'
    });

    MachineHistoryCard.belongsTo(models.User, {
      foreignKey: 'created_by',
      as: 'creator'
    });

    MachineHistoryCard.belongsTo(models.User, {
      foreignKey: 'updated_by',
      as: 'updater'
    });
  };

  return MachineHistoryCard;
};
