/*
 * STDC Segmentation Post-Processing for KL630
 * Matches Python stdc630inference.py logic
 */
#ifndef STDC_POST_PROCESS_H
#define STDC_POST_PROCESS_H

#include <stdint.h>
#include "ncpu_gen_struct.h"

/* Job ID - must match host_stream.ini [nnm] JobId */
#define STDC_JOB_ID           200

/* Model ID from readme.txt */
#define STDC_MODEL_ID         32769

/* Number of segmentation classes */
#define STDC_NUM_CLASSES      8

/* Maximum seg map pixels (safe upper bound: 128 cols × 64 rows) */
#define STDC_SEG_MAP_MAX      8192

/* Class indices (from Python CLASS_LABELS dict) */
#define STDC_CLASS_BUNKER     0
#define STDC_CLASS_CAR        1
#define STDC_CLASS_GRASS      2
#define STDC_CLASS_GREENERY   3
#define STDC_CLASS_PERSON     4
#define STDC_CLASS_POND       5
#define STDC_CLASS_ROAD       6
#define STDC_CLASS_TREE       7

/* Warning thresholds (from Python code) */
#define STDC_WARNING_SECONDS  2.0f
#define STDC_MOTION_THRESHOLD 3.0f
#define STDC_TREE_GROWTH_THR  0.05f
#define THR_GRASS_ROI         0.30f   /* grass ratio in forward ROI */
#define THR_CAR_GLOBAL        0.05f
#define THR_PERSON_GLOBAL     0.01f
#define THR_GREENERY_GLOBAL   0.20f
#define THR_BUNKER_GLOBAL     0.01f
#define THR_POND_GLOBAL       0.01f
#define THR_TREE_DENSE        0.30f

/* Collision ROI boundaries (fraction of image) - from Python COL_ROI_* */
#define COL_ROI_LEFT          0.25f
#define COL_ROI_RIGHT         0.75f
#define COL_ROI_TOP           0.25f
#define COL_ROI_BOTTOM        0.70f

#define THR_PERSON_COLLISION  0.01f
#define THR_CAR_COLLISION     0.05f
#define THR_TREE_COLLISION    0.20f
#define THR_BUNKER_COLLISION  0.01f
#define THR_POND_COLLISION    0.01f

/* Forward ROI trapezoid - from Python roi_pts */
#define FWD_ROI_TOP_LEFT      0.45f
#define FWD_ROI_TOP_RIGHT     0.55f
#define FWD_ROI_BOTTOM_LEFT   0.30f
#define FWD_ROI_BOTTOM_RIGHT  0.70f
#define FWD_ROI_TOP           0.55f
#define FWD_ROI_BOTTOM        0.95f

typedef struct {
    float fps;
} stdc_post_proc_params_t;

/* Per-class analysis result */
typedef struct {
    float    class_ratio[STDC_NUM_CLASSES]; /* global ratio per class */
    float    grass_roi_ratio;               /* grass ratio in forward ROI */
    float    tree_roi_ratio;                /* tree ratio in forward ROI */
    float    tree_roi_growth;               /* delta vs previous frame */
    float    col_person_ratio;              /* collision ROI ratios */
    float    col_car_ratio;
    float    col_tree_ratio;
    float    col_bunker_ratio;
    float    col_pond_ratio;
    float    grass_duration_sec;            /* consecutive on-grass duration */
    float    motion_diff;                   /* mean luma diff in forward ROI */
    uint8_t  is_moving;        /* 1 if ROI motion exceeds threshold */
    uint8_t  on_grass;         /* 1 if on_grass detected */
    uint8_t  grass_warning;    /* 1 if on grass for warning duration */
    uint8_t  person_warning;   /* 1 if person > threshold */
    uint8_t  car_warning;      /* 1 if car > threshold */
    uint8_t  greenery_warning; /* 1 if greenery > threshold */
    uint8_t  bunker_warning;   /* 1 if bunker > threshold */
    uint8_t  pond_warning;     /* 1 if pond > threshold */
    uint8_t  collision_risk;   /* 1 if collision risk in center ROI */
    uint8_t  tree_approaching; /* 1 if tree ROI grows too fast */
    uint8_t  tree_dense;       /* 1 if tree dense area */
    uint32_t frame_count;      /* analyzed frame count */
    uint32_t seg_width;        /* segmentation output width */
    uint32_t seg_height;       /* segmentation output height */
} stdc_analysis_t;

typedef struct {
    stdc_analysis_t analysis;
    uint8_t seg_map[STDC_SEG_MAP_MAX]; /* argmax class per pixel, row-major */
} stdc_result_t;

/* Post-processing function - matches VMF NNM post_proc_func signature */
int stdc_post_process(int model_id, struct kdp_image_s *image_p);

#endif /* STDC_POST_PROCESS_H */
