/* Independent ABI1 scene lifecycle example. Caller owns UI and resource transport. */
#include "sqs_map.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void ok(int32_t code){if(code){fprintf(stderr,"%s\n",sqsm_map_last_error());exit(1);}}
static char *take(sqsm_map_bytes_t out){char *value=malloc((size_t)out.len+1);assert(value);memcpy(value,out.data,(size_t)out.len);value[out.len]=0;ok(sqsm_map_bytes_free(out.allocation));return value;}
static char *scene(sqsm_map_t map,const char *request){sqsm_map_bytes_t out={0};ok(sqsm_map_scene_json(map,(const uint8_t*)request,strlen(request),&out));return take(out);}
int main(void){
 sqsm_map_t map=0;ok(sqsm_map_create(128,96,&map));sqsm_map_bytes_t out={0};ok(sqsm_map_capabilities_json(map,&out));char *caps=take(out);assert(strstr(caps,"\"sceneReceiverOwnership\":true")&&strstr(caps,"\"scenePlayback\":false"));free(caps);
 char *created=scene(map,"{\"method\":\"create\",\"args\":[{\"id\":\"network\"}]}");const char *prefix="\"sceneInstanceId\":\"",*id=strstr(created,prefix);assert(id);id+=strlen(prefix);const char *end=strchr(id,'"');assert(end&&end-id<80);char identity[80];memcpy(identity,id,(size_t)(end-id));identity[end-id]=0;assert(strspn(identity,"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-:")==strlen(identity));free(created);
 /* Bind live receivers through source JSON with owner:{id,sceneInstanceId} before start.
  * Honor source resource starts/cancels on the map creation thread; no networking in core. */
 const char *methods[]={"getState","start","pause","resume","stop","remove"};
 for(size_t i=0;i<sizeof methods/sizeof methods[0];i++){char request[256];snprintf(request,sizeof request,"{\"method\":\"%s\",\"args\":[{\"id\":\"network\",\"sceneInstanceId\":\"%s\"}]}",methods[i],identity);char *snapshot=scene(map,request);assert(strstr(snapshot,"\"id\":\"network\""));free(snapshot);}
 char *list=scene(map,"{\"method\":\"list\",\"args\":[]}");assert(strcmp(list,"[]")==0);free(list);ok(sqsm_map_destroy(map));puts("C scene lifecycle and capability JSON: passed");return 0;
}
