/**
 * @file task.hpp
 * @brief Task component header - Manages system tasks and their lifecycle
 * @author Mahmoud Elmohtady
 * @company Nabd solutions - ASF
 * @copyright Copyright (c) 2025
 */

#ifndef TASK_HPP
#define TASK_HPP

#include <cstdint>
#include <cstddef>

/**
 * @brief Task priority levels
 */
enum class TaskPriority
{
    LOW = 1,
    NORMAL = 5,
    HIGH = 10,
    CRITICAL = 20
};

/**
 * @brief Task state enumeration
 */
enum class TaskState
{
    CREATED,
    READY,
    RUNNING,
    BLOCKED,
    SUSPENDED,
    DELETED
};

/**
 * @brief Task configuration structure
 */
struct TaskConfig
{
    const char* name;           ///< Task name
    uint32_t stackSize;         ///< Stack size in bytes
    TaskPriority priority;      ///< Task priority
    uint32_t coreId;           ///< CPU core ID (0 or 1 for ESP32)
};

/**
 * @brief Task class
 * 
 * Provides functionality to create, manage, and control tasks
 * with scheduling, prioritization, and resource management.
 */
class Task
{
public:
    /**
     * @brief Constructor
     */
    Task();

    /**
     * @brief Destructor
     */
    ~Task();

    /**
     * @brief Create a new task
     * @param config Task configuration
     * @param taskFunction Function to run in the task
     * @param arg Argument to pass to task function
     * @return true if task created successfully, false otherwise
     */
    bool create(const TaskConfig& config, void (*taskFunction)(void*), void* arg);

    /**
     * @brief Delete the task
     * @return true if deleted successfully, false otherwise
     */
    bool destroy();

    /**
     * @brief Suspend the task
     * @return true if suspended successfully, false otherwise
     */
    bool suspend();

    /**
     * @brief Resume the task
     * @return true if resumed successfully, false otherwise
     */
    bool resume();

    /**
     * @brief Get the current task state
     * @return Current task state
     */
    TaskState getState() const;

    /**
     * @brief Get the task priority
     * @return Task priority level
     */
    TaskPriority getPriority() const;

    /**
     * @brief Set the task priority
     * @param priority New priority level
     * @return true if priority set successfully, false otherwise
     */
    bool setPriority(TaskPriority priority);

    /**
     * @brief Delay task execution
     * @param delayMs Delay in milliseconds
     */
    static void delay(uint32_t delayMs);

    /**
     * @brief Get current running task handle
     * @return Task handle or nullptr if not running
     */
    static Task* getCurrentTask();

private:
    TaskState m_state;
    TaskPriority m_priority;
    void* m_handle;
};

#endif // TASK_HPP

