<?php

use EasyFlow\Master\Models\Flow;
use EasyFlow\Master\Models\Task;
use EasyFlow\Master\Models\User;
use EasyFlow\Master\Controllers\LogController;
use EasyFlow\Master\Controllers\MailController;
require_once dirname(__DIR__) . "/includes/configuration.php";
try {
    $flows = Flow::all();
    foreach ($flows as $flow) {
        $admin = User::find($flow->flow_administrator);
        $open_tasks = Task::where('task_flow', $flow->flow_id)->where(function ($query) {
            $query->open();
        })->with(['request', 'step', 'user'])->get();
        $html = view('email.daily_admin_summary', compact('flow', 'admin', 'open_tasks'));
        MailController::sendGeneralMail([["email" => $admin->user_email]], "Resumen de tareas abiertas - " . $flow->flow_name . " - " . date("Y-m-d"), $html);
    }
    LogController::logCron('Cron ejecutado con éxito (JobDailyAdminSummary.php)', [], 'INFO');
} catch (Exception $e) {
    LogController::logCron('Error al ejecutar cron (JobDailyAdminSummary.php)', ['error' => $e->getMessage()], 'ERROR');
}