<?php

namespace App\Console\Commands;

use App\Libs\Import\Import;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Log;
use RequestContext;

class ImportApi extends Command
{
    private $types = [
        'Themes',
        'Typologies',
        'Locations',
        'Categories',
        'Accesses',
        'Areas',
        "Inscriptions",
        "Classifications",
        //"Places",
        "Roles",
        "PropertyTypes",
        "Properties",
        "Epigraphs",
        "Clean",
        "GenerateSitemap"
    ];

    private $typesToClean = [
        'Themes',
        'Typologies',
        'Accesses',
        'Classifications',
        'PropertyTypes',
        'Locations',
        'Chronologies'
    ];

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'importApi:import {--all}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Import from InPatrimonium';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $importAll = $this->option('all');
        if ($importAll) {
            foreach ($this->types as $type) {
                try {
                    $this->_callImport($type);
                    $this->info("=== " . $type . " Imported successfully ===");
                } catch (\Throwable $e) {
                    $this->info("=== The Type '" . $type . "' Failed to Import ===");
                    $this->warn($e);
                }
            }
            $errors = (Array)RequestContext::get('importErrors');
            if (empty($errors)) {
                Log::stack(['importLogStack', 'rollbar'])->info('The Import was a Success! Hooray!');
            } else {
                $output = implode(" | ", $errors);
                Log::stack(['importLogStack', 'rollbar'])->critical("The Import was a Success! However, there were some errors: {$output}");
            }
        } else {
            if (is_null(config("import.endpoint"))) {
                $this->info("=== There is no environment variable for the API endpoint (IN_PATRIMONION_API), make sure you set one. ===");
            } else {
                $importAll = $this->confirm('Do you want to import everything?');

                if ($importAll) {
                    $this->warn("This will import all the available information in the Api");
                    $confirmation = $this->confirm("Are you sure?");
                    if ($confirmation) {
                        $this->info("=== START IMPORTING EVERYTHING ===");
                        foreach ($this->types as $type) {
                            try {
                                $this->_callImport($type);
                                $this->info("=== " . $type . " Imported successfully ===");
                            } catch (\Throwable $e) {
                                $this->info("=== The Type '" . $type . "' Failed to Import ===");
                                $this->warn($e);
                            }
                        }
                    }
                } else {
                    $type = $this->choice('What do you want to import?', $this->types, 0);
                    if ($type === "Clean") {
                        $this->warn("This will clear all the data that has no relations(or so we think).");
                        $confirm = $this->confirm("Are you sure?");
                        if ($confirm) {
                            $this->_callImport($type);
                        }
                    } else {
                        $this->_callImport($type);
                        $this->info("=== " . $type . " Imported successfully ===");
                    }
                }
            }
        }
    }

    private function _callImport($type)
    {
        $import = new Import($type, $this->typesToClean);
        $import->start();
    }
}
