<?php

namespace App\Livewire\Admin\Translations;

use App\Models\Translation;
use Livewire\Component;

class Index extends Component
{
    public $locale = 'de';
    public $translations = [];

    public $search = '';
    public $onlyMissing = false;
    public $grouped = [];

    protected $listeners = ['translations:updated' => 'loadTranslations'];

    public function mount()
    {
        $this->loadTranslations();
    }

    public function updatedLocale()
    {
        $this->loadTranslations();
    }

    public function updatedSearch()
    {
        $this->loadTranslations();
    }

    public function updatedOnlyMissing()
    {
        $this->loadTranslations();
    }

    public function loadTranslations()
    {
        $query = Translation::query()->select('key')->distinct();

        // 🔍 SEARCH
        if ($this->search) {
            $query->where('key', 'like', '%' . $this->search . '%');
        }

        $keys = $query->pluck('key');

        $groups = [];

        foreach ($keys as $key) {

            $values = Translation::where('key', $key)
                ->pluck('value', 'locale')
                ->toArray();

            // 🔥 FILTER: NUR FEHLENDE
            if ($this->onlyMissing) {
                if (count($values) === count(config('app.locales'))) {
                    continue;
                }
            }

            $group = explode('.', $key)[0];

            $groups[$group][] = [
                'key' => $key,
                'values' => $values,
            ];
        }

        $this->grouped = $groups;
    }


    public function updateField($index, $field, $value)
    {
        $item = $this->translations[$index];

        if (!$item['id']) {
            $translation = Translation::create([
                'key' => $item['key'],
                'locale' => $this->locale,
                'value' => $value,
            ]);

            $this->translations[$index]['id'] = $translation->id;
            $this->translations[$index]['missing'] = false;
        } else {
            Translation::where('id', $item['id'])->update([
                $field => $value
            ]);
        }

        $this->translations[$index][$field] = $value;
    }


    public function updateValue($index, $value)
    {
        $item = $this->translations[$index];

        Translation::where('id', $item['id'])->update([
            'value' => $value
        ]);

        // lokal updaten
        $this->translations[$index]['value'] = $value;
    }

    public function addNew()
    {
        $translation = Translation::create([
            'key' => 'new.key.' . uniqid(),
            'locale' => $this->locale,
            'value' => '',
        ]);

        $this->translations[] = [
            'id' => $translation->id,
            'key' => $translation->key,
            'value' => '',
            'missing' => false, // 🔥 wichtig
        ];
    }


    public function deleteKey($key)
    {
        Translation::where('key', $key)->delete();

        $this->loadTranslations();
    }

    public function render()
    {
        return view('livewire.admin.translations.index')
            ->layout('layouts.app');
    }
}
