<?php
/**
 * Setup Seeder
 *
 * Multi-Tenant-Seeder, der über die Setup-Konfiguration in "etc/setup/globals.php" (im Projekt) gesteuert wird.
 *
 * @version     1.0.$Revision:$
 * @version     SVN: $Id:$
 * @package     bplan-modules/visitor-management
 * @subpackage  Seeders
 * @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\Database\Seeders\Project;

use BplanBase\Globals\Enums\LocationSiteSubType;
use BplanBase\Globals\Models\Location;
use BplanBase\Globals\Models\Tenant;
use BplanBase\Globals\Models\LocationType;
use BplanModules\VisitorManagement\Enums\LocationAccessPointSubType;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;


/**
 *
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 */
class LocationSeeder extends Seeder
{

    /**
     * Run the database seeds.
     *
     * @return void
     *
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function run()
    {
        $date = date('Y-m-d H:i:s');

        $locationTypes = LocationType::get()
            ->pluck('id', 'identifier')
            ->all();

        $globalsSetup = \BplanBase\Globals\Database\Seeders\DatabaseSeeder::loadConfig();

        foreach ($globalsSetup as $tenant => $globalsConfig) {
            $ProjectTenant = Tenant::where('identifier', $tenant)->first();

            Location::insert([
                [
                    'uuid'             => Str::uuid(),
                    'active'           => 1,
                    'created_at'       => $date,
                    'updated_at'       => $date,
                    'address_addition' => $globalsConfig['location']['address-addition'],
                    'city'             => $globalsConfig['location']['city'],
                    'email'            => $globalsConfig['company']['email'],
                    'house_number'     => $globalsConfig['location']['house-number'],
                    'identifier'       => $ProjectTenant->tenantRecord->short_name.'-'.LocationSiteSubType::Headquarters->name,
                    'internal'         => 1,
                    'labels'           => json_encode($globalsConfig['location']['labels']),
                    'location_type_id' => LocationType::where('identifier', '=', 'Site') //->where('sub_type', '=', 'Headquarters')
                        ->first()->id,

                    'master_id'        => null,
                    'path'             => '00',
                    'phone_number'     => $globalsConfig['company']['phone-number'],
                    'street'           => $globalsConfig['location']['street'],
                    'sub_type'         => LocationSiteSubType::Headquarters->name,
                    'tenant_id'        => $ProjectTenant->id,
                    'top_level'        => 1,
                    'zip_code'         => $globalsConfig['location']['zip-code'],
                ]
            ]);
            if ($ProjectTenant->identifier === 'System') {
                /*
                **  Backend-Location unter dem System-Tenant anlegen. */
                $supportedLanguages = array_keys(config('globals.supported-languages'));

                $labels = array_combine($supportedLanguages, array_fill(0, count($supportedLanguages), 'Backend'));

                Location::insert([
                    [
                        'uuid'             => Str::uuid(),
                        'active'           => 1,
                        'created_at'       => $date,
                        'updated_at'       => $date,
                        'address_addition' => null,
                        'city'             => null,
                        'email'            => null,
                        'house_number'     => null,
                        'identifier'       => config('globals.backend-location-identifier'),
                        'internal'         => 1,
                        'labels'           => json_encode($labels, JSON_UNESCAPED_UNICODE),
                        'location_type_id' => LocationType::where('identifier', '=', 'AccessPoint') //->where('sub_type', '=', 'Headquarters')
                            ->first()->id,

                        'master_id'        => null,
                        'path'             => Location::nextPath('00'),
                        'phone_number'     => null,
                        'street'           => null,
                        'sub_type'         => LocationAccessPointSubType::Backend->name,
                        'tenant_id'        => $ProjectTenant->id,
                        'top_level'        => 0,
                        'zip_code'         => null,
                    ]
                ]);
            }
        }
    } // run()

} // class LocationSeeder extends Seeder {}
