#ifndef MOTOR_H
#define MOTOR_H

#include <string>
#include <vector>
#include <utility> // for std::pair

/**
 * @brief Represents a rocket motor with thrust characteristics over time.
 *
 * The Motor class manages the thrust curve, ignition timing, burn duration,
 * and remaining propellant mass of a single propulsion unit.
 */
class Motor {
public:
    /**
     * @brief Constructs a new Motor with a given name and mass properties.
     * @param name Name of the motor.
     * @param initialPropellantMass Initial mass of propellant (kg).
     * @param totalImpulse Total impulse (Ns) for basic verification.
     */
    Motor(const std::string& name, double initialPropellantMass, double totalImpulse);

    /**
     * @brief Default destructor.
     */
    ~Motor() = default;

    /**
     * @brief Adds a thrust curve data point.
     *
     * Should be called during setup. Time must be monotonically increasing.
     *
     * @param time Time since ignition (seconds).
     * @param thrust Thrust at this time (Newtons).
     */
    void addThrustDataPoint(double time, double thrust);

    /**
     * @brief Starts motor ignition (sets internal ignition time to zero).
     */
    void ignite();

    /**
     * @brief Updates the motor status based on elapsed time.
     * @param deltaTime Time step in seconds.
     */
    void update(double deltaTime);

    /**
     * @brief Gets the current thrust output.
     * @return Current thrust in Newtons.
     */
    double getCurrentThrust() const;

    /**
     * @brief Checks whether the motor has burned out.
     * @return True if burnout has occurred.
     */
    bool isBurnedOut() const;

    /**
     * @brief Returns the remaining propellant mass.
     * @return Remaining propellant mass in kilograms.
     */
    double getRemainingPropellantMass() const;

    /**
     * @brief Gets the name of the motor.
     * @return Motor name as a constant reference.
     */
    const std::string& getName() const;

private:
    std::string name_; ///< Name of the motor.

    double initialPropellantMass_; ///< Initial propellant mass [kg].
    double remainingPropellantMass_; ///< Current propellant mass [kg].

    double totalImpulse_; ///< Total impulse [Ns], used for sanity checks.

    std::vector<std::pair<double, double>> thrustCurve_; ///< Thrust curve (time, thrust) points.

    double ignitionTime_; ///< Elapsed time since ignition [s].
    bool ignited_; ///< Whether the motor has been ignited.
    bool burnedOut_; ///< Whether the motor has finished burning.

    double currentThrust_; ///< Cached current thrust [N].

    /**
     * @brief Linearly interpolates thrust from the thrust curve.
     * @param time Time since ignition (seconds).
     * @return Interpolated thrust (Newtons).
     */
    double interpolateThrust(double time) const;
};

#endif // MOTOR_H