<?php
declare(strict_types=1);

namespace App\Modules\Workflow\Models;

use App\Core\App;

class WorkflowTransitionLog
{
    public static function create(array $data): int
    {
        $db = App::getInstance()->db();
        return $db->insert('workflow_transition_log', $data);
    }

    public static function getForInstance(int $instanceId): array
    {
        $db = App::getInstance()->db();
        return $db->select(
            "SELECT wtl.*, e.full_name_ar as employee_name FROM workflow_transition_log wtl LEFT JOIN employees e ON e.id = wtl.triggered_by_employee_id WHERE wtl.workflow_instance_id = ? ORDER BY wtl.created_at ASC",
            [$instanceId]
        );
    }

    public static function getLatest(int $instanceId): ?array
    {
        $db = App::getInstance()->db();
        return $db->selectOne(
            "SELECT * FROM workflow_transition_log WHERE workflow_instance_id = ? ORDER BY created_at DESC LIMIT 1",
            [$instanceId]
        );
    }
}