<?php

declare(strict_types=1);

namespace ImovelZapAi\Automation\Application\Queries;

use ImovelZapAi\Automation\Domain\Enums\ExecutionStatus;
use ImovelZapAi\Automation\Domain\Enums\WorkflowStatus;
use ImovelZapAi\Automation\Infrastructure\Persistence\Eloquent\AutomationExecutionModel;
use ImovelZapAi\Automation\Infrastructure\Persistence\Eloquent\AutomationWorkflowModel;

final class GetAutomationDashboardQuery
{
    /** @return array<string, int|float> */
    public function execute(string $tenantId): array
    {
        $workflows = AutomationWorkflowModel::query()->where('tenant_id', $tenantId);
        $executions = AutomationExecutionModel::query()->where('tenant_id', $tenantId);

        return [
            'workflows_total' => (int) (clone $workflows)->count(),
            'workflows_active' => (int) (clone $workflows)->where('status', WorkflowStatus::Active)->count(),
            'workflows_paused' => (int) (clone $workflows)->where('status', WorkflowStatus::Paused)->count(),
            'executions_total' => (int) (clone $executions)->count(),
            'executions_succeeded' => (int) (clone $executions)->where('status', ExecutionStatus::Succeeded)->count(),
            'executions_failed' => (int) (clone $executions)->where('status', ExecutionStatus::Failed)->count(),
            'executions_skipped' => (int) (clone $executions)->where('status', ExecutionStatus::Skipped)->count(),
            'executions_last_24h' => (int) (clone $executions)->where('created_at', '>=', now()->subDay())->count(),
        ];
    }
}
