<?php

namespace Database\Seeders;

use App\Models\Backoffice\WebsiteObject;
use App\Models\Backoffice\WebsiteObjectLanguage;
use App\Models\Cpv;
use App\Models\CpvLanguage;
use App\Models\Department;
use App\Models\DepartmentLanguage;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Throwable;

class CpvsSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $seederData = json_decode(
            Storage::disk('json')->get('cpvs.json'),
            true
        );

        $languages = LaravelLocalization::getSupportedLanguagesKeys();

        DB::beginTransaction();
        try {
            foreach ($seederData as $data) {
                if (strlen($data['cpvCode']) < 3) {
                    // these will be our departments
                    continue;
                }

                $cpv = new Cpv();
                $cpv->full_code = $data['cpvCode'];
                $cpv->code = substr($data['cpvCode'], 0, 8);
                if ($cpv->code == '00000000') {
                    $cpv->code = null;
                }
                $cpv->migration_object_id = $data['oo_id'];
                $cpv->save();

                $wo = new WebsiteObject();
                $wo->object_id = $cpv->id;
                $wo->object_type = Cpv::class;
                $wo->content_type = 'cpv';
                $wo->save();

                foreach ($languages as $lang) {
                    $cpvLanguage = new CpvLanguage();
                    $cpvLanguage->cpv_id = $cpv->id;
                    $cpvLanguage->language_id = $lang;
                    $cpvLanguage->title = $data['cpvDescription'];
                    $cpvLanguage->save();

                    $wol = new WebsiteObjectLanguage();
                    $wol->website_object_id = $wo->id;
                    $wol->wo_language_id = $lang;
                    $wol->save();
                }
            }

            foreach ($seederData as $data) {
                if (strlen($data['cpvCode']) > 3) {
                    // these were the real cpvs, already migrated
                    continue;
                }

                $department = new Department();
                $department->code = $data['cpvCode'];
                $department->save();

                $wo = new WebsiteObject();
                $wo->object_id = $department->id;
                $wo->object_type = Department::class;
                $wo->content_type = 'department';

                if (!empty($data['cpvParent1'])) {
                    $parent = Department::full()
                        ->where('code', $data['cpvParent1'])
                        ->first();
                    if ($parent) {
                        $wo->parent_id = $parent->website_object_id;
                    }
                }

                $wo->save();

                foreach ($languages as $lang) {
                    $departmentLanguage = new DepartmentLanguage();
                    $departmentLanguage->department_id = $department->id;
                    $departmentLanguage->language_id = $lang;
                    $departmentLanguage->title = $data['cpvDescription'];
                    $departmentLanguage->save();

                    $wol = new WebsiteObjectLanguage();
                    $wol->website_object_id = $wo->id;
                    $wol->wo_language_id = $lang;
                    $wol->save();
                }

                if (!empty($data['cpvItems'])) {
                    $cpvRelations = explode(',', $data['cpvItems']);
                    $cpvObjectIds = [];
                    foreach ($cpvRelations as $cpvRelation) {
                        if (empty($cpvRelation)) {
                            continue;
                        }
                        $cpvObjectIds[] = intval(explode('|', $cpvRelation)[1]);
                    }
                    $syncCpvs = Cpv::whereIn('migration_object_id', $cpvObjectIds)->pluck('id');
                    $department->cpvs()->sync($syncCpvs);
                }
            }

            DB::commit();
        } catch (Throwable $e) {
            DB::rollBack();
            throw($e);
        }
    }
}
