#pragma once

#include <cstdint>
#include <memory>
#include <vector>

#include "core/audio_backend.h"
#include "core/device_discovery.h"
#include "core/diagnostic_log.h"

namespace aptt {

struct KernelStreamingPcmPolicy {
  bool requires_explicit_filter_pin = true;
  bool requires_fresh_capability_evidence = true;
  bool keep_warm_after_signal = true;
  bool release_after_drain = false;
  bool uses_reset_on_normal_stop = false;
  std::uint32_t packet_duration_ms = 10;
  std::uint32_t packets_per_write = 1;
  std::uint32_t release_cooldown_ms = 1000;
};

KernelStreamingPcmPolicy MakeKernelStreamingPcmPolicy(
    const PlaybackConfig& config);
bool IsKernelStreamingPcmPlaybackFormat(const FormatDescriptor& format);
bool IsKernelStreamingDopCarrierFormat(const FormatDescriptor& format);
bool IsKernelStreamingPlaybackFormat(const FormatDescriptor& format);
bool IsKernelStreamingConfirmedPlaybackProbe(
    const CapabilityProbeRecord& probe);
const char* KernelStreamingRunningSilenceRestartRequiredMessage();
std::vector<std::uint8_t> ConvertFloat32InterleavedToKernelStreamingPcmBytes(
    const std::vector<float>& interleaved_samples,
    std::uint32_t channel_count,
    const FormatDescriptor& format);

class KernelStreamingPcmBackend final : public IAudioBackend {
 public:
  KernelStreamingPcmBackend();
  ~KernelStreamingPcmBackend() override;

  BackendApi api() const override;
  DeviceList enumerateDevices() override;
  FormatProbeResult probeFormats(const DeviceInfo& device) override;
  OperationResult initialize(const PlaybackConfig& config) override;
  OperationResult start() override;
  OperationResult stop() override;
  OperationResult reset() override;
  RealtimeStats getRealtimeStats() const override;
  BackendError getLastError() const override;
  OperationResult shutdown() override;

  DeviceLifecycleState lifecycle_state() const;
  std::vector<DiagnosticEvent> diagnostic_events() const;

 private:
  struct Impl;
  std::unique_ptr<Impl> impl_;
};

}  // namespace aptt
