<?php

namespace limble\helpers;

use classes\limble;
use Exception;

/**
 * {
 * "taskID": 8203, // The unique identifier for the item the webhook event describes
 * "status": "COMPLETE", // The status (e.g. task created, task complete, etc.) -- see below for a full list of possible statuses.
 * "category": "task", // The category of item that triggered the webhook to fire (e.g. task, po, poItem).
 * "user": "bob@limblecmms.com" // The user whose action caused the webhook to fire.
 * }
 */
class limble_webhook_payload_task extends limble_webhook_payload
{
    /**
     * The task object
     * @var string $task
     */
    public string $task = limble_task::class;
    /**
     * The Task ID
     * @var int $taskID
     */
    public int $taskID;
    /**
     * The status of the task
     * @var string $status
     */
    public string $status;
    /**
     * The category of the task
     * @var string $category
     */
    public string $category;
    /**
     * The user who triggered the webhook
     * @var string $user
     */
    public string $user;
    /**
     * The task object
     * @var limble_task $taskObject
     */
    public limble_task $taskObject;

    /**
     * Constructor for the limble_webhook_payload_task class
     * @throws Exception If the task ID is not provided or is invalid
     */
    public function __construct(array $payload)
    {
        parent::__construct($payload);
        $this->taskID = (int)($payload['taskID'] ?? 0);
        $this->status = (string)($payload['status'] ?? '');
        $this->category = (string)($payload['category'] ?? '');
        $this->user = (string)($payload['user'] ?? '');
        $this->taskObject = new limble_task(((new limble())->getTask($payload['taskID']))[0] ?? []);
        $this->notifyWebhook();
    }

    protected function notifyWebhook(): void
    {
        // Generate the message to notify the webhook
        $message = "";
        $message .= "Task ID: " . $this->taskID . "\n";
        $message .= "Name: " . $this->taskObject->name . "\n";
        $message .= "Description: " . $this->taskObject->description . "\n";
        $message .= "User: " . $this->user . "\n";
        $message .= "Status: " . $this->status . "\n";
        $message .= "Category: " . $this->category . "\n";
        // Notify the webhook with the task object
        $this->slack->send_message($message, 'Limble Webhook Task Notification');
    }

    /**
     * CREATED
     * DELETED
     * CHANGED DUE DATE
     * CHANGED ASSIGNMENT
     * COMPLETE
     * CHANGED COMPLETED TASK
     * COMPLETED TASK REOPENED
     * CHANGED TASK NAME
     * CHANGED TASK DESCRIPTION
     * ADDED COMMENT TO TASK
     * CUSTOM TAG ADDED TO TASK
     * CUSTOM TAG REMOVED FROM TASK
     * LOGGED TIME ON TASK
     */

    protected function CREATED(): void
    {
        // Notify the webhook that a task has been created
        $this->slack->send_message('Task Created: ' . $this->taskObject->name, 'Limble Webhook Task Created');
    }

    protected function DELETED(): void
    {
        // Notify the webhook that a task has been deleted
        $this->slack->send_message('Task Deleted: ' . $this->taskObject->name, 'Limble Webhook Task Deleted');
    }

    protected function CHANGED_DUE_DATE(): void
    {
        // Notify the webhook that the due date of the task has changed
        $this->slack->send_message('Task Due Date Changed: ' . $this->taskObject->name, 'Limble Webhook Task Due Date Changed');
    }

    protected function CHANGED_ASSIGNMENT(): void
    {
        // Notify the webhook that the assignment of the task has changed
        $this->slack->send_message('Task Assignment Changed: ' . $this->taskObject->name, 'Limble Webhook Task Assignment Changed');
    }

    protected function COMPLETE(): void
    {
        // Notify the webhook that the task has been completed
        $this->slack->send_message('Task Completed: ' . $this->taskObject->name, 'Limble Webhook Task Completed');
    }

    protected function CHANGED_COMPLETED_TASK(): void
    {
        // Notify the webhook that a completed task has been changed
        $this->slack->send_message('Completed Task Changed: ' . $this->taskObject->name, 'Limble Webhook Completed Task Changed');
    }

    protected function CHANGED_TASK_NAME(): void
    {
        // Notify the webhook that the task name has changed
        $this->slack->send_message('Task Name Changed: ' . $this->taskObject->name, 'Limble Webhook Task Name Changed');
    }


}