BareGit
#pragma once

#include <cstdint>
#include <optional>
#include <string>
#include <vector>

#include <mw/error.hpp>

#include "probe_status.h"

/// One recorded probe result, independent of endpoint configuration.
struct StatusRecord
{
    /// Unique service ID from the configuration.
    std::string service_id;
    /// UTC Unix timestamp in whole seconds.
    std::int64_t timestamp;
    /// Nonnegative probe duration in microseconds.
    std::int64_t duration_microsecond;
    /// Outcome of the probe.
    ProbeStatus status;

    /// Compare all persisted fields.
    bool operator==(const StatusRecord&) const = default;
};

/// Thread-safe access to probe history; failures are returned as mw::E errors.
/// Missing or stale data is interpreted as NA by the caller.
class DataSourceInterface
{
public:
    /// Release the data source through its interface.
    virtual ~DataSourceInterface() = default;

    /// Append a probe result to the service's history.
    virtual mw::E<void> save(const StatusRecord& record) = 0;

    /// Fetch the newest timestamp; ties use the most recently saved result.
    /// Return an empty optional when the service has no history.
    virtual mw::E<std::optional<StatusRecord>> latest(
        const std::string& service_id) = 0;

    /// Read [start, end) in timestamp order, then insertion order for ties.
    /// Bounds are UTC Unix seconds. Reversed bounds return an error.
    virtual mw::E<std::vector<StatusRecord>> history(
        const std::string& service_id, std::int64_t start,
        std::int64_t end) = 0;
};