<?php
/**
 * EncryptEnvValue Command Class
 *
 * @version     1.0.0
 * @package     bplan-base/globals
 * @subpackage  Commands
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 * @copyright   Copyright (C) 2025, 2026 bplan-solutions GmbH & Co. KG <https://www.bplan-solutions.de/>
 * /Δ\
 */

namespace BplanBase\Globals\Console\Commands;


use BplanBase\Globals\Env\EnvCrypt;
use Illuminate\Console\Command;


/**
 * @version     1.1.0 / 2026-07-22
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 */
class EncryptEnvValue extends Command
{


/* +++ TRAITS +++ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */


/* +++ CLASS CONSTANTS +++ ++++++++++++++++++++++++++++++++++++++++++++++++++ */


/* +++ OBJECT MEMBERS +++ +++++++++++++++++++++++++++++++++++++++++++++++++++ */


    /**
     * @var     string
     */
    protected $description = 'Encrypts a plain-text value for safe storage in the .env file';


    /**
     * @var     string
     */
    protected $signature = 'globals:encrypt-env-value {value? : The plain-text value to encrypt}';


/* +++ CLASS MEMBERS +++ ++++++++++++++++++++++++++++++++++++++++++++++++++++ */


/* +++ OBJECT METHODS +++ +++++++++++++++++++++++++++++++++++++++++++++++++++ */


    /**
     * @return      int
     *
     * @version     1.0.0 / 2026-04-26
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function handle(): int
    {
        $plainValue = $this->argument('value');

        if ($plainValue === null) {
            $plainValue = $this->secret('Enter the value to encrypt');
        }
        if ($plainValue === null || $plainValue === '') {
            $this->error('No value provided.');

            return Command::FAILURE;
        }
        $encrypted = EnvCrypt::encrypt($plainValue);

        $this->newLine();
        $this->line('Encrypted value (copy this into your .env file):');
        $this->newLine();
        $this->line('"'.$encrypted.'"');
        $this->newLine();

        return Command::SUCCESS;

    } // handle()


/* +++ CLASS METHODS +++ ++++++++++++++++++++++++++++++++++++++++++++++++++++ */


} // class EncryptEnvValue extends Command {}
