<?php

namespace App\Console\Commands;

use App\Libs\Services\LanguageService;
use Illuminate\Console\Command;
use WebsiteObjectLanguage;

class CloneTranslations extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'content:clone-translations {languageCode} {sourceLanguageCode}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Creates or replaces all site translations for a given language with the default or specified one';

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

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle(LanguageService $service)
    {
        $languageId = $this->argument('languageCode');
        $sourceLanguageId = $this->argument('sourceLanguageCode');
        if (!$languageId) {
            $this->error('You have to specify the language code (e.g. pt)');
            return 0;
        }
        $count = WebsiteObjectLanguage::where('wo_language_id', $languageId)->count();
        if ($count === 0 || $this->confirm('Seems like the translations for ' . $languageId . ' already exist ('
            . $count . ' object entries translated). Do you wish to continue and replace all contents for the default language?')) {
            $service->createOrResetTranslations($languageId, $sourceLanguageId);
        }
        return 0;
    }
}
