#ifndef BYTEOR_V1_H
#define BYTEOR_V1_H

#pragma once

/* ByteOr v1 extends the IndexBus v1 ABI and requires indexbus_v1.h on the include path. */
#include "indexbus_v1.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
 * Region supports an events chain layout: one slot pool shared by multiple SPSC queues.
 */
#define BYTEOR_CAP_SUPPORTS_EVENTS_CHAIN (1u << 16)

/**
 * Region supports an events chain layout with per-edge MPSC queues.
 */
#define BYTEOR_CAP_SUPPORTS_EVENTS_CHAIN_MPSC (1u << 18)

/**
 * Region supports a sequenced-slots layout: sequencer cursor + slot pool + ring of slot indices.
 */
#define BYTEOR_CAP_SUPPORTS_SEQUENCED_SLOTS (1u << 17)

/**
 * `LayoutHeader.flags` kind value for the ByteOr events-chain layout (`EventsChainLayout4`).
 */
#define BYTEOR_REGION_KIND_EVENTS_CHAIN 100

/**
 * `LayoutHeader.flags` kind value for the ByteOr sequenced-slots layout (`SequencedSlotsLayout4`).
 */
#define BYTEOR_REGION_KIND_SEQUENCED_SLOTS 101

/**
 * Concrete v1 consumer count used by `SequencedSlotsLayout4`.
 */
#define BYTEOR_SEQUENCED_SLOTS_CONSUMERS_DEFAULT 4

/**
 * Concrete v1 events chain layout for `Q=4`.
 *
 * This is a convenient "known size" layout for tools and smoke tests.
 */
typedef struct {
  /**
   * Common region header (magic/version/capabilities/layout size).
   */
  LayoutHeader header;
  /**
   * 0 = uninitialized, 1 = initializing, 2 = initialized
   */
  IndexbusAtomicU32 initialized;
  /**
   * Padding (reserved).
   */
  uint32_t _pad0;
  /**
   * Pad to 64 so the first 64-byte-aligned section starts at offset 64.
   */
  uint8_t _pad_to_64[40];
  /**
   * Shared slot pool for payload bytes.
   */
  SlotPoolLayout slot_pool;
  /**
   * Per-edge SPSC queues of slot indices.
   */
  IndexQueue queues[4];
  /**
   * Per-edge MPSC queues of slot indices.
   */
  MpscQueue mpsc_queues[4];
} INDEXBUS_ALIGNED(64) EventsChainLayout4;

/**
 * Concrete v1 sequenced-slots layout for `N=4`.
 *
 * This is a convenient "known size" layout for tools and smoke tests.
 */
typedef struct {
  /**
   * Common region header (magic/version/capabilities/layout size).
   */
  LayoutHeader header;
  /**
   * 0 = uninitialized, 1 = initializing, 2 = initialized
   */
  IndexbusAtomicU32 initialized;
  /**
   * Padding (reserved).
   */
  uint32_t _pad0;
  /**
   * Pad to 64 so the first counter starts at offset 64.
   */
  uint8_t _pad_to_64[40];
  /**
   * Producer cursor (monotonic sequence).
   */
  IndexbusAtomicU64 cursor;
  /**
   * Padding to keep the cursor in its own cache line.
   */
  uint8_t _pad_cursor[56];
  /**
   * Per-consumer gating sequences.
   */
  SequencerGatingCell gating[BYTEOR_SEQUENCED_SLOTS_CONSUMERS_DEFAULT];
  /**
   * Slot pool storing payload bytes.
   */
  SlotPoolLayout slot_pool;
  /**
   * Ring mapping `seq -> slot index`.
   */
  IndexbusAtomicU32 ring[INDEXBUS_QUEUE_CAPACITY];
} INDEXBUS_ALIGNED(64) SequencedSlotsLayout4;

#ifdef __cplusplus
} /* extern "C" */
#endif

/* ---- ABI sanity checks (generated) ---- */

#if defined(__cplusplus) && (__cplusplus >= 201103L)
    static_assert(alignof(EventsChainLayout4) == 64, "EventsChainLayout4 align mismatch");
    static_assert(sizeof(EventsChainLayout4) % 64 == 0, "EventsChainLayout4 size must be 64B-multiple");
    static_assert(offsetof(EventsChainLayout4, slot_pool) == 64, "EventsChainLayout4.slot_pool offset mismatch");
    static_assert(offsetof(EventsChainLayout4, queues) == 278656, "EventsChainLayout4.queues offset mismatch");
    static_assert(offsetof(EventsChainLayout4, mpsc_queues) == 295296, "EventsChainLayout4.mpsc_queues offset mismatch");

    static_assert(alignof(SequencedSlotsLayout4) == 64, "SequencedSlotsLayout4 align mismatch");
    static_assert(sizeof(SequencedSlotsLayout4) % 64 == 0, "SequencedSlotsLayout4 size must be 64B-multiple");
    static_assert(offsetof(SequencedSlotsLayout4, cursor) == 64, "SequencedSlotsLayout4.cursor offset mismatch");
    static_assert(offsetof(SequencedSlotsLayout4, gating) == 128, "SequencedSlotsLayout4.gating offset mismatch");
#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
    _Static_assert(_Alignof(EventsChainLayout4) == 64, "EventsChainLayout4 align mismatch");
    _Static_assert(sizeof(EventsChainLayout4) % 64 == 0, "EventsChainLayout4 size must be 64B-multiple");
    _Static_assert(offsetof(EventsChainLayout4, slot_pool) == 64, "EventsChainLayout4.slot_pool offset mismatch");
    _Static_assert(offsetof(EventsChainLayout4, queues) == 278656, "EventsChainLayout4.queues offset mismatch");
    _Static_assert(offsetof(EventsChainLayout4, mpsc_queues) == 295296, "EventsChainLayout4.mpsc_queues offset mismatch");

    _Static_assert(_Alignof(SequencedSlotsLayout4) == 64, "SequencedSlotsLayout4 align mismatch");
    _Static_assert(sizeof(SequencedSlotsLayout4) % 64 == 0, "SequencedSlotsLayout4 size must be 64B-multiple");
    _Static_assert(offsetof(SequencedSlotsLayout4, cursor) == 64, "SequencedSlotsLayout4.cursor offset mismatch");
    _Static_assert(offsetof(SequencedSlotsLayout4, gating) == 128, "SequencedSlotsLayout4.gating offset mismatch");
#endif

#endif  /* BYTEOR_V1_H */
