<?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\Core\Tenant\Modals;


use BplanBase\Globals\Http\Requests\UpdateTenantRecordRequest;
use BplanBase\Globals\Http\Requests\UpdateTenantRequest;
use BplanBase\Globals\Models\Tenant;
use BplanBase\Globals\Models\TenantRecord;
use BplanBase\Globals\Repositories\CountryRepository;
use BplanBase\Globals\Services\TenantRecordService;
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-25
 * @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 $city = null;


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


    /**
     * @var     array<int, string>
     */
    public array $countries = [];


    /**
     * @var     int|null
     */
    public ?int $country_id = null;


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


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


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


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


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


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


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


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


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


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


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


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


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


    /**
     * @var     TenantRecord|null
     */
    public ?TenantRecord $_TenantRecord = null;


    /**
     * @var     TenantRecordService
     */
    protected TenantRecordService $_TenantRecordService;


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


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


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


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


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

    } // boot()


    /**
     *
     * @param       array   $tenant
     * @param       string  $openTab
     *
     * @version     1.0.0 / 2026-05-25
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    #[On('show-edit-tenant-modal')]
    public function initTenant(array $tenant, string $openTab = 'base'): void
    {
        $tenantId            = $tenant['id'];
        $this->_Tenant       = Tenant::withoutGlobalScopes()->find($tenantId);
        $this->_MasterTenant = $this->_Tenant?->masterTenant;
        $this->_TenantRecord = TenantRecord::withoutGlobalScopes()->find($tenantId);

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

        $this->city                = $this->_TenantRecord->city;
        $this->company             = $this->_TenantRecord->company;
        $this->country_id          = $this->_TenantRecord->country_id;
        $this->description         = $this->_TenantRecord->description;
        $this->email               = $this->_TenantRecord->email;
        $this->home_page           = $this->_TenantRecord->home_page;
        $this->house_number        = $this->_TenantRecord->house_number;
        $this->mobile_phone_number = $this->_TenantRecord->mobile_phone_number_country_code.$this->_TenantRecord->mobile_phone_number;
        $this->phone_number        = $this->_TenantRecord->phone_number_country_code.$this->_TenantRecord->phone_number;
        $this->short_name          = $this->_TenantRecord->short_name;
        $this->street              = $this->_TenantRecord->street;
        $this->zip_code            = $this->_TenantRecord->zip_code;

        $this->dispatch('tenant-modal-initialized',
            phoneNumber:       $this->phone_number,
            mobilePhoneNumber: $this->mobile_phone_number,
            openTab:           $openTab,
        );

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

    } // initTenant()


    /**
     *
     * @version     1.0.0 / 2026-05-25
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function mount(): void
    {
        $this->countries = CountryRepository::getLocalizedList();

    } // mount()


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

    } // render()


    /**
     *
     * @version     1.0.0 / 2026-05-25
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function resetModal(): void
    {
        $this->active              = false;
        $this->city                = null;
        $this->company             = null;
        $this->country_id          = null;
        $this->description         = null;
        $this->domain              = null;
        $this->email               = null;
        $this->home_page           = null;
        $this->house_number        = null;
        $this->internal            = false;
        $this->mobile_phone_number = null;
        $this->phone_number        = null;
        $this->short_name          = null;
        $this->street              = null;
        $this->zip_code            = null;
        $this->_MasterTenant       = null;
        $this->_Tenant             = null;
        $this->_TenantRecord       = null;

        $this->resetValidation();

    } // resetModal()


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

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

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

        $tenantRecordRules = UpdateTenantRecordRequest::getRules($this->_TenantRecord->id);

        unset($tenantRecordRules['id']);
        unset($tenantRecordRules['mobile_phone_number_country_code']);
        unset($tenantRecordRules['phone_number_country_code']);

        return array_merge($tenantRules, $tenantRecordRules);

    } // rules()


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

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

            $this->_TenantRecordService->update($this->_TenantRecord, [
                'city'                 => $this->city,
                'company'              => $this->company,
                'country_id'           => $this->country_id,
                'description'          => $this->description,
                'email'                => $this->email,
                'home_page'            => $this->home_page,
                'house_number'         => $this->house_number,
                'mobile_phone_number'  => $this->mobile_phone_number,
                'phone_number'         => $this->phone_number,
                'short_name'           => $this->short_name,
                'street'               => $this->street,
                'zip_code'             => $this->zip_code,
            ]);

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

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

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

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

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

    } // save()


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


} // class Edit extends Component {}
