#pragma once

#include <memory>
#include <vector>
#include <string>
#include <opencv2/opencv.hpp>
#include <libcamera/libcamera.h>

// Forward declarations
class CameraHandler {
public:
    CameraHandler();
    ~CameraHandler();
    
    // Initialize camera system
    bool initialize();
    
    // Configure the camera with specific resolution
    bool configureCamera(int width = 3280, int height = 2464);
    
    // Capture a single image
    bool captureImage(const std::string& outputFolder = "./captured_images/");
    
    // Process the captured image (to grayscale, apply effects, etc.)
    bool processImage(const std::string& outputFolder = "./captured_images/");
    
    // Clean up resources
    void cleanup();

private:
    static void requestComplete(libcamera::Request *request);
    
    // Internal camera objects
    std::unique_ptr<libcamera::CameraManager> cm;
    std::shared_ptr<libcamera::Camera> camera;
    libcamera::Stream *stream;
    
    // Buffer handling
    libcamera::FrameBufferAllocator *allocator;
    std::vector<std::unique_ptr<libcamera::Request>> requests;
    const std::vector<std::unique_ptr<libcamera::FrameBuffer>> *buffers;
    
    // Configuration
    std::unique_ptr<libcamera::CameraConfiguration> config;
    libcamera::StreamConfiguration *streamConfig;
    
    // Image data
    void *imageData;
    size_t dataSize;
    cv::Mat capturedImage;
    
    // Status flags
    bool cameraInitialized;
    bool captureCompleted;
}; 