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

#include <QObject>
#include <QString>
#include <QVariant>

#include "types/schedulerconfig.h"
#include "types/flexibleload.h"
#include "types/energytimeslot.h"
#include "types/manualslotconfig.h"

// Persists SchedulerManager mutable state to:
//   NymeaSettings::settingsPath() + "/scheduler.conf"  (QSettings INI)
//
// Persisted sections:
//   [scheduler]    strategyId, SchedulerConfig fields
//   [loads]        per-ThingId LoadConfig (priority, deadline, targetValue)
//   [overrides]    active manual overrides (slotStart → loadSource + powerW + reason)
class SchedulerSettings : public QObject
{
    Q_OBJECT
public:
    explicit SchedulerSettings(QObject *parent = nullptr);

    // Active strategy identifier (e.g. "rule-based", "ai-ml")
    QString  activeStrategyId() const;
    void     setActiveStrategyId(const QString &id);

    // Scheduler algorithm configuration
    SchedulerConfig schedulerConfig() const;
    void            setSchedulerConfig(const SchedulerConfig &config);

    // Per-load configuration — keyed by ThingId string
    struct LoadConfig {
        double    priority    = 0.5;
        QDateTime deadline;
        double    targetValue = 0;
        bool      enabled     = true;
    };

    LoadConfig loadConfig(const ThingId &thingId) const;
    void       setLoadConfig(const ThingId &thingId, const LoadConfig &config);
    QList<ThingId> configuredLoadIds() const;

    // Active overrides — keyed by slotStart (ISO string)
    struct OverrideEntry {
        LoadSource source    = LoadSource::External;
        double     powerW    = 0;
        QString    reason;
        QDateTime  expiresAt; // optional; empty = no expiry
    };

    QList<QDateTime>  overrideSlotStarts() const;
    OverrideEntry     overrideEntry(const QDateTime &slotStart) const;
    void              setOverride(const QDateTime &slotStart, const OverrideEntry &entry);
    void              removeOverride(const QDateTime &slotStart);
    void              clearExpiredOverrides();

    // Manual slot configurations (used by ManualStrategy)
    QList<ManualSlotConfig> manualSlots() const;
    void setManualSlot(const ManualSlotConfig &config);
    void removeManualSlot(const QDateTime &start);
    void clearManualSlots();

private:
    QString settingsFilePath() const;

    QString         m_activeStrategyId = QStringLiteral("rule-based");
    SchedulerConfig m_config;
    QHash<QString, LoadConfig>    m_loadConfigs;
    QHash<QString, OverrideEntry> m_overrides;
    QList<ManualSlotConfig>       m_manualSlots;

    void load();
    void save();
};

#endif // SCHEDULERSETTINGS_H
