<?php

namespace App\Livewire\Admin\Taxonomy;

use Livewire\Component;
use Illuminate\Support\Str;
use App\Models\Admin\Language;
use App\Models\Admin\Taxonomy;
use Livewire\Attributes\Validate;

class Create extends Component
{
    public $selectedLanguage;
    public $defaultLanguage;

    public $selectedCategory;

    public $translations = [];

    #[Validate('required|min:2')]
    public $name;

    public $slug;
    public $handle;

    #[Validate('required')]
    public $isSingleSelect = 1;

    public $content;
    public $image_path;

    public function __construct()
    {
        $this->selectedCategory = request()->get('id');

        $this->defaultLanguage = Language::where('default', 1)->first()->short;
        $this->selectedLanguage = $this->defaultLanguage;

        $this->translations = Language::where('status', 1)
            ->orderByDesc('default')
            ->pluck('short')
            ->mapWithKeys(function ($short) {
                return [$short => ['name' => '', 'slug' => '', 'content' => '']];
            })
            ->toArray();
    }

    public function changeLanguage($language)
    {
        $this->selectedLanguage = $language;

        $this->name = $this->translations[$language]['name'];
        $this->content = $this->translations[$language]['content'];

        $this->dispatch('languageChanged', content: $this->content);
    }

    public function updatedName($value)
    {
        $this->slug = Str::slug($value);

        $this->translations[$this->selectedLanguage]['name'] = $value;
    }

    public function updatedContent($value)
    {
        $this->translations[$this->selectedLanguage]['content'] = $value;
    }

    public function save()
    {
        $this->validate();

        $taxonomy = new Taxonomy();

        foreach ($this->translations as $language => $translation) {
            $taxonomy->setTranslation('name', $language, $translation['name']);
            $taxonomy->setTranslation('slug', $language, $translation['slug']);
            $taxonomy->setTranslation('content', $language, $translation['content']);

            if ($language == $this->defaultLanguage) {
                $taxonomy->handle = Str::snake($translation['name']);
            }
        }

        $taxonomy->parent_id = $this->selectedCategory;

        $taxonomy->isSingleSelect= $this->isSingleSelect;

        $fullPath = $this->image_path;
        $taxonomy->image_path = get_relative_image_path($fullPath);

        $taxonomy->save();

        activity()->performedOn($taxonomy)->causedBy(auth()->id())->log('Kategori oluşturuldu');

        $this->dispatch('showAlert', [
            'title' => 'Başarılı!', 'text' => 'Kategori başarıyla oluşturuldu.', 'icon' => 'success'
        ]);

        $this->resetExcept('selectedCategory');
    }

    public function saveAndRedirect()
    {
        $this->save();
        return redirect()->route('admin.taxonomy.index', ['id' => $this->selectedCategory]);
    }

    public function render()
    {
        return view('livewire.admin.taxonomy.create');
    }
}
