<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Facades\Services\ImportService;
use App\Exceptions\ImportException;
use Illuminate\Support\Facades\DB;
use Exception;
use Illuminate\Support\Facades\Log;
use App\Models\Property;
use App\Models\Epigraph;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Refresh Properties and Epigraphs website object URI';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        DB::beginTransaction();
        try {
            ImportService::refreshUri([
                'properties' => Property::class,
                'epigraphs' => Epigraph::class
            ]);
            DB::commit();
        } catch (Exception $e) {
            DB::rollback();
            Log::stack(['importLogStack', 'rollbar'])->error('Caught an exception refreshing websiteObject Uris ---- RefreshWebsiteUri<handle>');
            throw new ImportException($e);
        }
    }
}
