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

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

#include <integrations/thing.h>

// How the load can be controlled by the Scheduler
enum class LoadType {
    Inflexible, // Always active, not manageable (base load)
    Shiftable,  // Must complete before deadline (EV, washing machine)
    Modulable,  // Power can be adjusted (heat pump, AC)
    Storage     // Bidirectional energy vector (battery, DHW tank)
};

// Which subsystem this load belongs to
enum class LoadSource {
    SmartCharging,  // Managed by SmartChargingManager (EV)
    HeatPump,       // Managed by HeatPumpManager
    DHW,            // Managed by DHW (domestic hot water)
    Battery,        // Managed by BatteryManager
    FeedIn,         // Grid feed-in
    External        // Future: third-party plugin
};

// Represents a manageable energy consumer/producer.
// The Scheduler allocates power to each FlexibleLoad in the timeline.
struct FlexibleLoad {
    ThingId  thingId;
    QString  displayName;
    LoadType  type   = LoadType::Inflexible;
    LoadSource source = LoadSource::External;

    double minPowerW     = 0;
    double maxPowerW     = 0;
    double currentPowerW = 0;

    QDateTime deadline;       // For Shiftable: must complete before this time
    double targetValue  = 0;  // SOC% (battery/EV) or °C (DHW/HP)
    double currentValue = 0;

    double priority     = 0.5; // 0.0 (low) – 1.0 (critical)

    bool   manualOverride  = false;
    QString overrideReason;

    bool isNull() const { return thingId.isNull(); }

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

Q_DECLARE_METATYPE(FlexibleLoad)

// Human-readable names for serialisation / JSON-RPC
QString loadTypeToString(LoadType type);
LoadType loadTypeFromString(const QString &str);
QString loadSourceToString(LoadSource source);
LoadSource loadSourceFromString(const QString &str);

QDebug operator<<(QDebug debug, const FlexibleLoad &load);

#endif // FLEXIBLELOAD_H
