<?php

namespace App\Console\Commands\Launchpad;

use App\Models\Language\LanguageTranslation;
use App\Models\Launchpad\Launchpad;
use Carbon\Carbon;
use Illuminate\Console\Command;

class LaunchpadStateMonitorCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'launchpad:state-monitor';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $launchpads = Launchpad::where('status', true)->get();

        foreach ($launchpads as $launchpad) {
            // Check pending launchpads

            $started = $launchpad->start_time->lessThanOrEqualTo(Carbon::now());
            $finished = $launchpad->end_time->lessThanOrEqualTo(Carbon::now());

            if($launchpad->progress == "pending" && $started && !$finished) {
                $launchpad->progress = "open";
                $launchpad->purchasable = true;
                $launchpad->update();
            } elseif($finished && $launchpad->progress !== "closed") {
                $launchpad->progress = "closed";
                $launchpad->purchasable = false;
                $launchpad->update();
            }
        }
    }
}
