<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\EpigraphLanguage;
use App\Models\PropertyLanguage;
use App\Facades\Services\ImportService;
use Illuminate\Support\Facades\Cache;
use Throwable;

class GenerateGlossaryTerms extends Command
{
    private $allowedTagsWithSup = '<b><i><a><br><sup>';
    private $tagsWithoutLinks = '<b><i><br><sup>';

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'importApi:generateTerms';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'This command regenerates glossary terms for properties and epigraphs';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $langs = getAvailableLanguages();
        $errors = [];
        $epigraphs = EpigraphLanguage::all();
        $properties = PropertyLanguage::all();
        $this->info("=== Regenerating properties description with glossary terms ===");
        foreach($properties as $property) {
            try {
                $noLinks = strip_tags($property->description, $this->tagsWithoutLinks);
                $newDesc = ImportService::formatForGlossary(ImportService::decodeItemText($noLinks, $this->allowedTagsWithSup), $property->language_id);
                if ($newDesc !== $property->description) {
                    $property->description = $newDesc;
                    $property->save();
                }
            } catch (Throwable $e) {
                $errors[] = "Property ID(" . $property->property_id . ") failed to regenerate glossary terms\n";
            }
        }
        $this->info("=== Properties regeneration is complete! ===");
        $this->info("=== Regenerating epigraphs description with glossary terms ===");
        foreach($epigraphs as $epigraph) {
            try {
                $noLinks = strip_tags($epigraph->description, $this->tagsWithoutLinks);
                $newDesc = ImportService::formatForGlossary(ImportService::decodeItemText($noLinks, $this->allowedTagsWithSup), $epigraph->language_id);
                if ($newDesc !== $epigraph->description) {
                    $epigraph->description = $newDesc;
                    $epigraph->save();
                }
            } catch (Throwable $e) {
                $errors[] = "Epigraph ID(" . $epigraph->epigraph_id . ") failed to regenerate glossary terms\n";
            }
        }
        $this->info("=== Epigraphs regeneration is complete! ===");
        foreach ($langs as $lang => $value) {
            Cache::forget("properties-{$lang}");
        }
        $this->info("=== Cleared Properties and Epigraphs cache! ===");
        $errorLog = implode(" ", $errors);
        if (!empty($errors)) {
            $this->info("=== ERROR LOG ===");
            $this->info($errorLog);
            $this->info("=== END OF ERROR LOG ===");
        }
    }
}
