#pragma once
#include <array>
#include <cstdint>
#include <cstring>
#include <cstdio>
namespace wearable {
template<size_t N> struct BoundedText {
    char value[N]{};
    void set(const char* s) {
        std::snprintf(value,N,"%s",s?s:"");
        if(s && std::strlen(s)>=N) {size_t n=N-1;while(n && (static_cast<unsigned char>(s[n])&0xc0)==0x80)--n;value[n]=0;}
    }
    const char* c_str() const {return value;}
    bool empty() const {return !*value;}
};
struct CatalogItem {
    BoundedText<65> project;
    BoundedText<9> agent;
    BoundedText<161> session,id;
    BoundedText<641> name;
    std::array<BoundedText<33>,16> efforts{};
    uint8_t effort_count=0;
};
struct AgentNavigator {
    enum class Page { Closed,Projects,Sessions,Agent,Model,Effort,Permissions,FirstMessage,NativeInput,Busy,Error };
    Page page=Page::Closed,loading=Page::Closed;
    std::array<CatalogItem,4> items{};
    std::array<BoundedText<641>,32> options{};
    uint8_t count=0,index=0,option_count=0;
    int next_cursor=-1;
    uint64_t generation=0,ack_generation=0,deadline=0;
    BoundedText<65> request,snapshot,project,operation,native_request;
    BoundedText<161> native_question,model;
    BoundedText<9> agent;
    BoundedText<33> effort,permission;
    BoundedText<1001> status;
    bool ack_pending=false;
    bool free_text=false,secret=false;
    bool catalog=false,target_select=false,permissions=false,native=false,create=false,history=false;
    void clear_page() {items={};options={};count=index=option_count=0;next_cursor=-1;}
    bool next() {const unsigned n=page==Page::NativeInput?option_count+(free_text?1:0):count+(next_cursor>=0?1:0)+(page==Page::Sessions&&create?1:0);if(n) index=(index+1)%n;return n>0;}
    bool busy() const {return page==Page::Busy;}
};
inline const char* native_decision_label(const char* decision) {
    return !std::strcmp(decision,"accept")?"Approve once":!std::strcmp(decision,"acceptForSession")?"Approve for session":!std::strcmp(decision,"decline")?"Decline":!std::strcmp(decision,"cancel")?"Cancel turn":decision;
}
}
