<?php
/**
 * Base Setting Repository Class
 *
 * @version     1.0.$Revision:$
 * @version     SVN: $Id:$
 * @package     bplan-base/globals
 * @subpackage  Repositories
 * @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\Foundation\Repositories;


use BplanBase\Globals\Foundation\Repositories\BaseRepository;
use BplanBase\Globals\Models\Tenant;
use Illuminate\Database\Eloquent\Model;


/**
 * Base Setting Repository Class
 *
 * @version     1.1.0 / 2026-03-17
 * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 */
class BaseSettingRepository extends BaseRepository
{


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


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


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


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


    /**
     * @var     array
     */
    private static array $_cache = [];


    /**
     * @var     string
     */
    protected static string $_cacheKey;


    /**
     * @var     string
     */
    protected static string $_createActionClass;


    /**
     * @var     string
     */
    protected static $_modelClass;


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


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


    /**
     *
     * @param       string $setting
     *
     * @return      mixed
     *
     * @version     1.1.0 / 2026-03-17
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public static function get(string $setting): mixed
    {
        $tenantId = Tenant::current()->id;

        if (isset(self::$_cache[$tenantId][static::$_cacheKey])) {
            return self::$_cache[$tenantId][static::$_cacheKey][$setting];
        }
        $Settings = static::getSettings();

        return $Settings->{$setting};

    } // getSettings()


    /**
     *
     * @return      Model
     *
     * @version     1.1.0 / 2026-03-17
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public static function getSettings(): Model
    {
        $tenantId = Tenant::current()->id;

        if (isset(self::$_cache[$tenantId][static::$_cacheKey])) {
            return self::$_cache[$tenantId][static::$_cacheKey];
        }
        $Settings = static::$_modelClass::find($tenantId);

        if ($Settings !== null) {
            return self::$_cache[$tenantId][static::$_cacheKey] = $Settings;
        }
        /*
        **  Erzeugt ein Model, speichert es in der Datenbank und liest es wieder aus.
        **  Auf diese Weise wird das Model mit den Standardwerten der Datenbank versehen. */
        $CreateAction = new static::$_createActionClass($tenantId);

        return self::$_cache[$tenantId][static::$_cacheKey] = $CreateAction->execute($tenantId);

    } // getSettings()


    /**
     *
     * @param       int|string|Tenant $tenant
     *
     * @return      void
     *
     * @version     1.1.0 / 2026-03-17
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public static function resetCache(int|string|Tenant $tenant): void
    {
        $tenantId = parent::_resolveId($tenant);

        unset(self::$_cache[$tenantId][static::$_cacheKey]);

    } // resetCache()


} // class BaseSettingRepository extends BaseRepository {}
