<?php
/**
 * Livewire Component
 *
 * @version     1.0.$Revision:$
 * @version     SVN: $Id:$
 * @package     bplan-base/globals
 * @subpackage  LivewireComponents
 * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
 * @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\Livewire\PrinterType\Modals;


use BplanBase\Globals\Services\PrinterTypeService;
use BplanBase\Globals\Traits\InteractsWithBanner;
use Livewire\Component;


/**
 * Livewire Component
 *
 * @version     1.1.0 / 2026-05-10
 * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 */
class Create extends Component
{


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


    use InteractsWithBanner;


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


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


    /**
     * @var     bool
     */
    public bool $active = true;


    /**
     * @var     string|null
     */
    public ?string $brand = null;


    /**
     * @var     string|null
     */
    public ?string $configTemplate = null;


    /**
     * @var     string|null
     */
    public ?string $description = null;


    /**
     * @var     string|null
     */
    public ?string $identifier = null;


    /**
     * @var     string|null
     */
    public ?string $label = null;


    /**
     * @var     string|null
     */
    public ?string $model = null;


    /**
     * @var     string|null
     */
    public ?string $payloadTemplate = null;


    /**
     * @var     PrinterTypeService
     */
    protected PrinterTypeService $_Service;


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


    /**
     * @var     string
     */
    private static string $_logPrefix = 'PrinterType/Modals/Create';


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


    /**
     *
     * @param       PrinterTypeService $Service
     *
     * @version     1.0.0 / 2026-05-06
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function boot(PrinterTypeService $Service): void
    {
        $this->_Service = $Service;

    } // boot()


    /**
     *
     * @version     1.1.0 / 2026-05-10
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function createPrinterType(): void
    {
        $this->configTemplate  = empty($this->configTemplate)  ? '{}' : $this->configTemplate;
        $this->payloadTemplate = empty($this->payloadTemplate) ? '{}' : $this->payloadTemplate;

        $this->validate();

        try {
            $this->_Service->create([
                'active'           => $this->active,
                'brand'            => $this->brand,
                'config_template'  => json_decode($this->configTemplate),
                'description'      => $this->description,
                'identifier'       => $this->identifier,
                'label'            => $this->label,
                'model'            => $this->model,
                'payload_template' => json_decode($this->payloadTemplate),
            ]);

            $User = auth()->user();

            \AppLog::info('PrinterType ['.$this->label.'] created by ['.$User->name.' ('.$User->email.')]', self::$_logPrefix);

            $this->dispatch('init-tippyjs');
            $this->dispatch('printer-type-created');

        } catch (\Throwable $th) {
            \AppLog::error($th, self::$_logPrefix);

            $this->dangerBanner(__('globals::printer-types.error.create-error'));
        }

    } // createPrinterType()


    /**
     *
     * @version     1.0.0 / 2026-05-06
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function render()
    {
        return view('globals::livewire.printer-type.modals.create');

    } // render()


    /**
     *
     * @version     1.0.0 / 2026-05-06
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function resetModal(): void
    {
        $this->active          = true;
        $this->brand           = null;
        $this->configTemplate  = null;
        $this->description     = null;
        $this->identifier      = null;
        $this->label           = null;
        $this->model           = null;
        $this->payloadTemplate = null;

        $this->resetValidation();

    } // resetModal()


    /**
     *
     * @return      array
     *
     * @version     1.0.0 / 2026-05-06
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function rules(): array
    {
        return [
            'active'          => ['boolean'],
            'brand'           => ['required', 'string', 'max:255'],
            'configTemplate'  => ['nullable', 'string', 'json'],
            'description'     => ['nullable', 'string', 'max:255'],
            'identifier'      => ['required', 'string', 'max:255', 'unique:printer_types,identifier'],
            'label'           => ['required', 'string', 'max:255'],
            'model'           => ['required', 'string', 'max:255'],
            'payloadTemplate' => ['nullable', 'string', 'json'],
        ];

    } // rules()


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


} // class Create extends Component {}
