<?php
/**
 * Core TenantResolver Class
 *
 * @todo        Fertig ausprogrammieren.
 *
 * @version     1.0.$Revision:$
 * @version     SVN: $Id:$
 * @package     bplan-base/globals
 * @subpackage  MultiTenancy
 * @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\MultiTenancy;


use BplanBase\Globals\Enums\UserAccessLevel;
use BplanBase\Globals\Models\Tenant;
use BplanBase\Globals\Models\User;
use BplanBase\Globals\Repositories\Core\TenantLocaleRepository;
use BplanBase\Globals\Repositories\Core\TenantRepository;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use InvalidArgumentException;
use LogicException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;


/**
 * Core TenantResolver Class
 *
 * @version     1.0.0 / 2026-03-24
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 */
class CoreApiTenantInitializer
{


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


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


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


    /**
     * @var     bool
     */
    private bool $_authCheck;


    /**
     * @var     Collection
     */
    private Collection $_AvailableTenants;


    /**
     * @var     string
     */
    private string|null $_configTenant;


    /**
     * @var     Request
     */
    private Request $_Request;


    /**
     * @var     Store
     */
    private $_Session;


    /**
     * @var     bool
     */
    private bool $_singleTenantMode;


    /**
     * @var     User|null
     */
    private ?User $_User = null;


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


    /**
     * @var     string
     */
    private static string $_tenantQueryParameter = \BplanBase\Globals\PackageServiceProvider::TENANT_QUERY_PARAMETER;


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


    /**
     *
     * @param       Request $Request
     *
     * @param       User $User
     *
     * @version     1.0.0 / 2026-03-24
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function __construct(Request $Request, User $User)
    {
        $this->_init($Request, $User);

    } // __construct()


    /**
     * Initialisiert Objektvariablen
     *
     * @param       Request $Request
     *
     * @param       User $User
     *
     * @return      void
     *
     * @version     1.0.0 / 2026-03-24
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    private function _init(Request $Request, User $User): void
    {
        $this->_authCheck = auth()->check();
        $this->_configTenant = config('globals.global-tenant');
        $this->_Request = $Request;
        $this->_singleTenantMode = !empty($this->_configTenant);
        $this->_User = $User;

    } // _init()


    /**
     * Überträgt Session-Daten in die Registry
     *
     * In dieser Methode wird auch der aktuelle Mandant via Spatie verfügbar gemacht.
     *
     * @return      void
     *
     * @version     1.0.0 / 2026-03-24
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    private function _initRegistry(): void
    {
        // // $Tenant = $this->_Session->get('core.tenant');

        // // $Tenant->makeCurrent();

        // \Registry::put('core.authCheck', $this->_authCheck, true);
        // \Registry::put('core.user',      $this->_User);
        // \Registry::put('core.userId',    $this->_User->id, true);

    } // _initRegistry()


    /**
     * Setzt die Registry- und die Session-Daten zurück
     *
     * @return 	    void
     *
     * @version     1.0.0 / 2026-03-24
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    private function _reset(): void
    {
        \Registry::remove('core');

        $this->_Session->remove('core');

    } // _reset()


    /**
     * Verarbeitet den Request und initialisiert den Mandanten
     *
     * @return      void
     *
     * @version     1.0.0 / 2026-03-24
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function initialize(): void
    {
        if ($this->_configTenant !== null) {
            $Tenant = TenantRepository::getByVarious($this->_configTenant, ignoreRestriction: true);

        } else {
            $tenantUuid = $this->_Request->header('Api-Tenant-UUID');

            if (config('app.env') === 'production') {
                $Tenant = TenantRepository::getByUuid($tenantUuid);
            } else {
                $Tenant = TenantRepository::getByVarious($tenantUuid);
            }
        }
        // $this->_initRegistry();

        $Tenant->makeCurrent();


    } // initialize()


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


} // class CoreApiTenantInitializer {}
