<?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-base/globals
 * @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\Models\Country;
use BplanBase\Globals\Models\Location;
use BplanBase\Globals\Models\LocationType;
use BplanBase\Globals\Models\Module;
use BplanBase\Globals\Models\ModuleTenant;
use BplanBase\Globals\Models\Role;
use BplanBase\Globals\Models\Tenant;
use BplanBase\Globals\Models\TenantLocale;
use BplanBase\Globals\Models\TenantRecord;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;


/**
 * Seeder Class
 *
 * Seed system tenant with tenant record, locales, roles and main location.
 *
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 */
class TenantSeeder 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');

        $SystemTenant = null;

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

        foreach ($globalsSetup as $tenant => $globalsConfig) {
            if ($tenant === 'System') {
                $path = '00';
                $tenantId = null;

            } else {
                if ($SystemTenant === null) {
                    $SystemTenant = Tenant::where('identifier', 'System')->first();
                }
                $path = Tenant::nextPath($SystemTenant->path);
                $tenantId = $SystemTenant->id;
            }
            Tenant::insert([
                [
                    'uuid'       => $globalsConfig['uuid'] ?? Str::uuid(),
                    'active'     => 1,
                    'created_at' => $date,
                    'updated_at' => $date,
                    'identifier' => $tenant,
                    'master_id'  => $tenantId,
                    'path'       => $path,
                ],
            ]);
            $Country = Country::where('iso3166_alpha_2', '=', $globalsConfig['company']['country-code'])->first();
            $ProjectTenant = Tenant::where('identifier', $tenant)->first();

            TenantRecord::insert([
                [
                    'id'                        => $ProjectTenant->id,
                    'uuid'                      => $ProjectTenant->uuid,
                    'created_at'                => $date,
                    'updated_at'                => $date,
                    'city'                      => $globalsConfig['location']['city'],
                    'company'                   => $globalsConfig['company']['name'],
                    'country_id'                => $Country->id,
                    'description'               => $globalsConfig['company']['description'],
                    'email'                     => $globalsConfig['company']['email'],
                    'home_page'                 => $globalsConfig['company']['home-page'],
                    'house_number'              => $globalsConfig['location']['house-number'],
                    'phone_number'              => $globalsConfig['company']['phone-number'],
                    'phone_number_country_code' => $globalsConfig['company']['phone-number-country-code'],
                    'short_name'                => $globalsConfig['company']['short-name'],
                    'street'                    => $globalsConfig['location']['street'],
                    'zip_code'                  => $globalsConfig['location']['zip-code'],
                ],
            ]);

            $keyIndex = 1;
            $main = 1;

            foreach ($globalsConfig['company']['locales'] as $locale) {
                TenantLocale::insert([
                    [
                        'uuid'       => Str::uuid(),
                        'active'     => 1,
                        'created_at' => $date,
                        'key_index'  => $keyIndex++,
                        'locale'     => $locale,
                        'main'       => $main,
                        'tenant_id'  => $ProjectTenant->id,
                    ]
                ]);
                $main = 0;
            }

            Role::insert([
                // [
                //     'uuid'       => Str::uuid(),
                //     'active'     => 1,
                //     'created_at' => $date,
                //     'updated_at' => $date,
                //     'identifier' => 'SuperUser',
                //     'internal'   => 1,
                //     'labels'     => '{"de": "SuperUser", "en": "SuperUser", "it": "SuperUser"}',
                //     'tenant_id'  => $ProjectTenant->id,
                // ],
                [
                    'uuid'       => Str::uuid(),
                    'active'     => 1,
                    'created_at' => $date,
                    'updated_at' => $date,
                    'identifier' => 'Admin',
                    'internal'   => 0,
                    'labels'     => '{"de": "Administrator", "en": "Administrator", "it": "Amministratore"}',
                    'tenant_id'  => $ProjectTenant->id,
                ],
            ]);
            Location::insert([
                [
                    'uuid'             => Str::uuid(),
                    'active'           => 1,
                    'created_at'       => $date,
                    'updated_at'       => $date,
                    'address_addition' => $globalsConfig['location']['address-addition'],
                    'city'             => $globalsConfig['location']['city'],
                    'house_number'     => $globalsConfig['location']['house-number'],
                    'labels'           => json_encode($globalsConfig['location']['labels']),
                    'location_type_id' => LocationType::where('identifier', '=', 'Site') //->where('sub_type', '=', 'Headquarters')
                        ->first()->id,

                    'master_id' => null,
                    'path'      => '00',
                    'street'    => $globalsConfig['location']['street'],
                    'sub_type'  => 'Headquarters',
                    'tenant_id' => $ProjectTenant->id,
                    'top_level' => 1,
                    'zip_code'  => $globalsConfig['location']['zip-code'],
                ]
            ]);

        }

    } // run()

} // class TenantSeeder extends Seeder {}
