CREATE TABLE IF NOT EXISTS AgentOutboxActions (
    Id INTEGER PRIMARY KEY AUTOINCREMENT,

    ActionType TEXT NOT NULL,
    IdempotencyKey TEXT NOT NULL UNIQUE,

    SubjectType TEXT NOT NULL,
    SubjectId TEXT NOT NULL,

    PayloadJson TEXT NULL,

    LocalStatus TEXT NOT NULL,
    SyncStatus TEXT NOT NULL,

    CreatedAtUtc TEXT NOT NULL,
    LocalCompletedAtUtc TEXT NULL,

    LastSyncAttemptAtUtc TEXT NULL,
    SyncCompletedAtUtc TEXT NULL,

    SyncAttemptCount INTEGER NOT NULL DEFAULT 0,
    LastError TEXT NULL
);

CREATE INDEX IF NOT EXISTS IX_AgentOutboxActions_SyncStatus
ON AgentOutboxActions (SyncStatus);

CREATE INDEX IF NOT EXISTS IX_AgentOutboxActions_ActionType_Subject
ON AgentOutboxActions (ActionType, SubjectType, SubjectId);