#pragma once

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

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

namespace aptt {

struct WasapiExclusivePcmPolicy {
  bool requires_explicit_endpoint = true;
  bool requires_exact_format_support = true;
  bool keep_warm_after_signal = true;
  bool release_after_drain = false;
  bool uses_reset_on_normal_stop = false;
  bool uses_shared_fallback = false;
  bool uses_format_fallback = false;
  std::uint32_t buffer_duration_ms = 100;
  std::uint32_t periodicity_ms = 100;
  std::uint32_t render_poll_interval_ms = 5;
  bool event_callback_enabled = true;
  bool alignment_retry_enabled = true;
  std::uint32_t event_wait_timeout_ms = 2000;
  std::uint32_t release_cooldown_ms = 1000;
};

WasapiExclusivePcmPolicy MakeWasapiExclusivePcmPolicy(
    const PlaybackConfig& config);
bool IsWasapiExclusivePcmPlaybackFormat(const FormatDescriptor& format);
bool IsWasapiExclusiveDopCarrierFormat(const FormatDescriptor& format);
bool IsWasapiExclusivePlaybackFormat(const FormatDescriptor& format);
std::vector<std::uint8_t> ConvertFloat32InterleavedToWasapiPcmBytes(
    const std::vector<float>& interleaved_samples,
    std::uint32_t channel_count,
    const FormatDescriptor& format);

class WasapiExclusivePcmBackend final : public IAudioBackend {
 public:
  WasapiExclusivePcmBackend();
  ~WasapiExclusivePcmBackend() 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
