<?php

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class GlobalDbImportSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */

    public function run()
    {
        /**
         *  Notes from last import:
         * 
         *  - json got by mysql workbench
         *  - sorting made manually inside the json
         *  - storage folder moved manually
         *  - media sql table imported in local from old db
         *  - after finished, exported the entire db and imported
         * 
         * */ 

        // 
        $db = json_decode(file_get_contents(resource_path('data/mijnggz.json')), true);

        if (empty($db)) {
            return;
        }

        $tables = array_keys($db);

        $sortOrder = [
            'addresses',
            'checklist_categories',
            'checklists',
            'users',
            'roles',
            'role_user',
            'synonyms',
            'learning_products',
            'versions',
            'contact_persons',
            'course_notifications',
            'failed_jobs',
            'filters',
            'filter_items',
            'filter_items_associations',
            'learning_product_synonym',
            'media',
            'member_employees',
            'members',
            'migrations',
            'notifications',
            'password_resets',
            'personal_access_tokens',
            'websockets_statistics_entries',
            'accreditations',
            'checklist_versions',
        ];

        $sortedTables = [];

        $skippable = [
            'migrations',
            'course_notifications',
            'media',
        ];

        foreach ($db as $key => $value) {

            if (!in_array($key, $skippable)) {
                DB::table($key)->insert($value);
            }
        }

        $this->call(BranchSeeder::class);
    }
}
