// 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 MANUALSTRATEGY_H
#define MANUALSTRATEGY_H

#include "ischedulingstrategy.h"
#include "types/manualslotconfig.h"

// Community-tier scheduling strategy: the user defines all time windows and
// power allocations explicitly.
//
// For each forecast slot:
//   - If a ManualSlotConfig covers that slot: apply its allocations exactly.
//   - If a matching config exists but is expired: reason = "Créneau expiré — ignoré",
//     apply critical/inflexible loads for safety.
//   - If no config matches: apply critical/inflexible loads only, reason =
//     "Aucune configuration — charges critiques uniquement".
//
// decisionReason is never empty (contract from ISchedulingStrategy).
class ManualStrategy : public ISchedulingStrategy
{
    Q_OBJECT
public:
    explicit ManualStrategy(QObject *parent = nullptr);

    QString strategyId()  const override { return QStringLiteral("manual"); }
    QString displayName() const override { return QStringLiteral("Manuel"); }
    QString description() const override {
        return QStringLiteral(
            "L'utilisateur définit lui-même les créneaux de charge. "
            "Aucune automatisation — contrôle total.");
    }

    QList<EnergyTimeSlot> computeSchedule(
        const QList<EnergyTimeSlot> &forecast,
        const QList<FlexibleLoad>   &loads,
        const SchedulerConfig       &config) override;

    QString explainDecision(
        const EnergyTimeSlot &slot,
        const FlexibleLoad   &load) const override;

    // Manual slot management
    void setManualSlot(const ManualSlotConfig &slotConfig);
    void removeManualSlot(const QDateTime &slotStart);
    void clearAllManualSlots();
    QList<ManualSlotConfig> manualSlots() const;

private:
    // Apply critical/inflexible loads to a slot (safety fallback).
    // Sets decisionReason if it is still empty after processing.
    void applyInflexibleLoads(EnergyTimeSlot &slot,
                              const QList<FlexibleLoad> &loads) const;

    QList<ManualSlotConfig> m_manualSlots;
};

#endif // MANUALSTRATEGY_H
