<?php
/**
 * Livewire Component
 *
 * @version     1.0.$Revision:$
 * @version     SVN: $Id:$
 * @version     STUB: 1.0.0
 * @generated   2026-05-24 22:10:04
 * @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\Tenant\Modals;


use BplanBase\Globals\Http\Requests\UpdateTenantRequest;
use BplanBase\Globals\Models\Tenant;
use BplanBase\Globals\Services\TenantService;
use BplanBase\Globals\Traits\InteractsWithBanner;
use Livewire\Attributes\On;
use Livewire\Component;


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


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


    use InteractsWithBanner;


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


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


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


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


    /**
     * @var     bool
     */
    public bool $internal = false;


    /**
     * @var     Tenant|null
     */
    public ?Tenant $_MasterTenant = null;


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


    /**
     * @var     Tenant|null
     */
    public ?Tenant $_Tenant = null;


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


    /**
     * @var     string
     */
    private static string $_logPrefix = 'Tenant/Modals/Edit';


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


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

    } // boot()


    /**
     *
     * @version     1.0.0 / 2026-05-24
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function editTenant(): void
    {
        $this->validate();

        try {
            $this->_Service->update($this->_Tenant, [
                'active'   => $this->active,
                'domain'   => $this->domain,
                'internal' => $this->internal,
            ]);

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

            \AppLog::info('Tenant ['.$this->_Tenant->identifier.'] updated by ['.$User->name.' ('.$User->email.')]', self::$_logPrefix);

            $this->dispatch('init-tippyjs');
            $this->dispatch('tenant-edited');

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

            $this->dangerBanner(__('globals::tenants.error.update-error'));
        }

    } // editTenant()


    /**
     *
     * @param       Tenant $tenant
     *
     * @version     1.0.0 / 2026-05-24
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    #[On('show-edit-tenant-modal')]
    public function initTenant(Tenant $tenant): void
    {
        $this->_Tenant       = $tenant;
        $this->_MasterTenant = $tenant->masterTenant;

        $this->active    = (bool) $tenant->active;
        $this->domain    = $tenant->domain;
        $this->internal  = (bool) $tenant->internal;

        $this->dispatch('init-tippyjs');

    } // initTenant()


    /**
     *
     * @version     1.0.0 / 2026-05-24
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function render()
    {
        return view('globals::livewire.tenant.modals.edit');

    } // render()


    /**
     *
     * @version     1.0.0 / 2026-05-24
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function resetModal(): void
    {
        $this->active        = false;
        $this->domain        = null;
        $this->internal      = false;
        $this->_MasterTenant = null;
        $this->_Tenant       = null;

        $this->resetValidation();

    } // resetModal()


    /**
     *
     * @return      array
     *
     * @version     1.0.0 / 2026-05-24
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function rules(): array
    {
        if ($this->_Tenant === null) {
            return [];
        }

        $rules = UpdateTenantRequest::getRules($this->_Tenant->id);

        unset($rules['identifier']);
        unset($rules['master_id']);

        return $rules;

    } // rules()


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


} // class Edit extends Component {}
