<?php

namespace App\Jobs;

use App\Models\Project;
use App\ProjectLogger;
use Carbon\Carbon;
use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Symfony\Component\Process\Process;

class ProjectInterpolateGrowthModelJob implements ShouldQueue
{
    use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected Project $project;

    public $timeout = 600;

    public function __construct(Project $project)
    {
        ProjectLogger::log($project, __CLASS__ . __METHOD__);
        $this->project = $project;
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        ProjectLogger::log($this->project,'start interpolate growth model', [$this->project->name]);
        $command = [
            sprintf('%s/interpolate_growth_model.sh', base_path('../')),
            sprintf('--project_dir=%s', $this->project->download_path),
        ];

        $process = new Process($command);
        $process->setTimeout(600);
        $process->run();

        if (!$process->isSuccessful()) {
            ProjectLogger::log($this->project,  $process->getErrorOutput());
            return;
        }
        ProjectLogger::log($this->project, $process->getOutput());

    }
}
