// SPDX-License-Identifier: GPL-3.0-or-later

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright (C) 2013 - 2024, nymea GmbH
* Copyright (C) 2024 - 2025, chargebyte austria GmbH
*
* This file is part of nymea-energy-plugin-nymea.
*
* nymea-energy-plugin-nymea.s free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* nymea-energy-plugin-nymea.s distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with nymea-energy-plugin-nymea. If not, see <https://www.gnu.org/licenses/>.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#ifndef ENERGYTIMESLOT_H
#define ENERGYTIMESLOT_H

#include <QDebug>
#include <QDateTime>
#include <QStringList>
#include <QVariant>
#include <QMetaType>

// Represents one time slot (typically 15–60 min) in the energy planning timeline.
// Predictions are all optional (0 = not yet available / stub value).
// Allocations and results are computed by the active ISchedulingStrategy.
struct EnergyTimeSlot {

    // Time boundaries
    QDateTime start;
    QDateTime end;

    // Predictions (0 = stub / not yet available)
    double solarForecastW        = 0;   // Expected PV production (W)
    double baseConsumptionW      = 0;   // Expected base load without flexible loads (W)
    double heatingNeedW          = 0;   // Estimated heating demand (W)
    double dhwNeedL              = 0;   // Estimated domestic hot water need (litres)
    double electricityPrice      = 0;   // Spot/tariff price (EUR/kWh) from TariffManager
    double electricitySellPrice  = 0;   // Feed-in tariff (EUR/kWh)

    // Decisions computed by the Scheduler (may be overridden manually)
    double allocatedToEV         = 0;   // Power allocated to EV charging (W)
    double allocatedToHP         = 0;   // Power allocated to heat pump (W)
    double allocatedToDHW        = 0;   // Power allocated to domestic hot water (W)
    double allocatedToBattery    = 0;   // >0 battery charging, <0 battery discharging (W)
    double allocatedToFeedIn     = 0;   // Planned grid feed-in (W)

    // Derived results
    double netGridPowerW         = 0;   // >0 grid import, <0 grid export (W)
    double estimatedCostEUR      = 0;   // Estimated cost for this slot (negative = revenue)
    double selfSufficiencyPct    = 0;   // Self-sufficiency percentage for this slot

    // Explainability — required, must not be empty for active slots
    QString     decisionReason;         // Human-readable explanation (e.g. "Surplus solaire +2.3 kW")
    QStringList decisionRules;          // Machine-readable rule tags (e.g. ["SolarSurplus","PriceBelow0.08"])

    // Manual override flag (Scheduler skips re-optimising this slot when true)
    bool    manualOverride       = false;
    QString overrideReason;

    bool isNull() const;
    bool isActive(const QDateTime &dt = QDateTime::currentDateTimeUtc()) const;

    QVariantMap toJson() const;
    static EnergyTimeSlot fromJson(const QVariantMap &map);
};

Q_DECLARE_METATYPE(EnergyTimeSlot)

QDebug operator<<(QDebug debug, const EnergyTimeSlot &slot);

#endif // ENERGYTIMESLOT_H
