<?php

namespace Database\Seeders;

use FluxCms\Core\Models\CmsIndustry;
use Illuminate\Database\Seeder;

class CmsIndustrySeeder extends Seeder
{
    public function run(): void
    {
        $locales = ['de', 'en'];
        $industriesByLocale = [];

        foreach ($locales as $locale) {
            $path = lang_path("{$locale}/components.php");
            if (file_exists($path)) {
                $data = require $path;
                $industriesByLocale[$locale] = $data['industries_band']['industries'] ?? [];
            }
        }

        $deIndustries = $industriesByLocale['de'] ?? [];
        $enIndustries = $industriesByLocale['en'] ?? [];

        $maxCount = max(count($deIndustries), count($enIndustries));

        for ($i = 0; $i < $maxCount; $i++) {
            CmsIndustry::updateOrCreate(
                ['order' => $i],
                [
                    'name' => array_filter([
                        'de' => $deIndustries[$i] ?? null,
                        'en' => $enIndustries[$i] ?? null,
                    ]),
                    'is_published' => true,
                ]
            );
        }
    }
}
