<?php

namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Helpers\ReportClass;
use App\Models\Settings\ScheduleLog;

class ReportsCron extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'reprotsmail:cron';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'This command will mail the reports to the given email ids';

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

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $todaytime = date("Y-m-d H:i:s");
        $insertobj_scheduler = ScheduleLog::create([
            'command' => 'reprotsmail:cron',
            'command_log' => "reprotsmail scheduler started at ".$todaytime,
            'status' => 0,
            'starttime' => $todaytime,
        ]);
        $scheudleid = $insertobj_scheduler->id;
        ////////////////////////////////////////////////////////////////////////////
        $reports = new ReportClass;
        $putfiles_reports = $reports->facility_schedule_report(); 
        $sendfiles_reports = $reports->facility_mailsend_report();   
        $todaytimeend = date("Y-m-d H:i:s");
        $scheudlerlogupd = ScheduleLog::where('id',$scheudleid)->where('status',0)->first();
        if(isset($scheudlerlogupd))
        {
            $scheudlerlogupd->status = 1;
            $scheudlerlogupd->endtime = $todaytimeend;
            $scheudlerlogupd->save();
        } 
    }
}
