<?php
/**
 * Seeder Class
 *
 * @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 Database\Seeders\Globals;


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 CombinedTenantSeeder extends Seeder
{

    /**
     * Run the database seeds.
     *
     * @return void
     *
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function run()
    {
        $identifier = config('globals.global-tenant');

        $company = 'bplan-solutions GmbH & Co. KG';
        $companyShortName = 'bplan-solutions';
        $countryCode = 'DE';
        $description = null;
        $email = 'core@bplan-solutions.de';
        $homePage = 'https://www.bplan-solutions.de/';
        $locales = [
            'de',
            'en',
        ];
        $phoneNumber = '22039898980';
        $phoneNumberCountryCode = '+49';

        $location = [
            'address_addition' => null,
            'city' => 'Köln',
            'house_number' => '22',
            'labels' => [
                'de' => 'Zentrale',
                'en' => 'Main Office',
                'it' => 'Centrale',
            ],
            'street' => 'Max-Reichpietsch-Straße',
            'zip_code' => '51147',
        ];
        $modules = [
            //'Visit',
        ];

        //  -----------------------------------------------------------

        $date = date('Y-m-d H:i:s');

        $SystemTenant = Tenant::where('identifier', 'System')->first();

        Tenant::insert([
            [
                'uuid' => Str::uuid(),
                'active' => 1,
                'created_at' => $date,
                'updated_at' => $date,
                'identifier' => $identifier,
                'master_id' => $SystemTenant->id,
                'path' => Tenant::nextPath($SystemTenant->path),
            ],

        ]);
        $Country = Country::where('iso3166_alpha_2', '=', $countryCode)->first();

        $ProjectTenant = Tenant::where('identifier', $identifier)->first();

        TenantRecord::insert([
            [
                'id' => $ProjectTenant->id,
                'uuid' => $ProjectTenant->uuid,
                'created_at' => $date,
                'updated_at' => $date,
                'city' => $location['city'],
                'company' => $company,
                'country_id' => $Country->id,
                'description' => $description,
                'email' => $email,
                'home_page' => $homePage,
                'house_number' => $location['house_number'],
                'phone_number' => $phoneNumber,
                'phone_number_country_code' => $phoneNumberCountryCode,
                'short_name' => $companyShortName,
                'street' => $location['street'],
                'zip_code' => $location['zip_code'],
            ],
        ]);

        $keyIndex = 1;
        $main = 1;

        foreach ($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' => $location['address_addition'],
                'city' => $location['city'],
                'house_number' => $location['house_number'],
                'labels' => json_encode($location['labels']),
                'location_type_id' => LocationType::where('identifier', '=', 'Site') //->where('sub_type', '=', 'Headquarters')
                    ->first()->id,

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

        foreach ($modules as $module) {
            $Module = Module::where('identifier', '=', $module)->first();

            if ($Module === null) {
                continue;
            }
            ModuleTenant::insert([
                [
                    'uuid' => Str::uuid(),
                    'active' => 1,
                    'created_at' => $date,
                    'updated_at' => $date,
                    'module_id' => $Module->id,
                    'tenant_id' => $ProjectTenant->id,
                ]
            ]);
        }

    } // run()

} // class CombinedTenantSeeder extends Seeder {}
