<?php

namespace App\Console\Invokables;

use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
use WebsiteObject;
use WebsiteObjectLanguage;

class CheckScheduledPublications
{
    public function __invoke()
    {
        $now = Carbon::now();
        // Log::info('[Scheduled publications] Checking scheduled publications... ' . $now->getTimezone());
        $toPublishList = WebsiteObject::whereNotNull('publish_on')
            ->where('publish_on', '<=', $now)
            ->get();

        foreach ($toPublishList as $toPublish) {
            Log::info('[Scheduled publications] Going to publish content - WO id: ' . $toPublish->website_object_id .
                ' slug: ' . $toPublish->slug . ' Scheduled date: ' . $toPublish->publish_on);
            $toPublish->publish_on = null;
            $toPublish->status = 'published';
            $toPublish->save();
        }

        $toPublishList = WebsiteObjectLanguage::whereNotNull('lang_publish_on')
            ->where('lang_publish_on', '<=', $now)
            ->get();

        foreach ($toPublishList as $toPublish) {
            Log::info('[Scheduled publications] Going to publish ' . $toPublish->wo_language_id . ' translation - WO id: ' . $toPublish->website_object_id .
                ' slug: ' . $toPublish->lang_slug . ' Scheduled date: ' . $toPublish->lang_publish_on);
            $toPublish->lang_publish_on = null;
            $toPublish->lang_status = 'published';
            $toPublish->save();
        }
    }
}
