<?php

namespace App\Console\Commands\PropertyReviewService;

use App\Exceptions\ApiErrorException;
use App\Jobs\PropertyReviewServiceJob;
use App\Models\PropertyReviewChannelMapping;
use Carbon\Carbon;
use Google\Protobuf\Api;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Illuminate\Mail\Mailer;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Exception;

class PropertyReviewScheduleService extends Command
{
    protected $signature = 'cron:property-review-schedule-service';

    protected $description = '';


    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {

        $this->info(date('Y-m-d H:i:s') . ' START');


        $propertyReviewChannelMapping = PropertyReviewChannelMapping::where('status', 1)
            ->with('channel')
            ->with('property')
            ->get();

        $propertyReviewChannelMapping = $propertyReviewChannelMapping ? $propertyReviewChannelMapping->toArray() : null;

        foreach ($propertyReviewChannelMapping as $propertyReview) {

            if (Carbon::createFromTimestamp($propertyReview['created_at'])->dayName != Carbon::now()->dayName) {
                continue;
            }

            $this->info(date('Y-m-d H:i:s') . ' Property Review: ' . $propertyReview['property']['name'] . ' - ' . $propertyReview['channel']['name']);

            dispatch(new PropertyReviewServiceJob($propertyReview['property_id'], $propertyReview['channel_id']));

        }


        $this->info(date('Y-m-d H:i:s') . ' FINISHED');


    }
}
