<?php

namespace App\Livewire\Admin\Popup;

use Livewire\Component;
use App\Models\Admin\Page;
use App\Models\Admin\Language;
use App\Models\Admin\Popup;
use Livewire\Attributes\Computed;

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

    public $image;
    public $image_path;

    public $image_mobile;
    public $image_mobile_path;

    public $translations = [];

    public $title;
    public $description;
    public $popup_link;
    public $page_id;
    public $display_type;
    public $popup_display_scope;
    public $popup_display_freq;

    public function mount()
    {
        $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 => ['title' => '', 'description' => '', 'popup_link' => '']];
            })
            ->toArray();
    }

    #[Computed()]
    public function pages()
    {
        return Page::whereNull('parent_id')->with('children')->get();
    }

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

        $this->title = $this->translations[$language]['title'] ?? '';
        $this->description = $this->translations[$language]['description'] ?? '';
        $this->popup_link = $this->translations[$language]['popup_link'] ?? '';

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

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

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

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

    public function save()
    {
        $popup = new Popup();

        foreach ($this->translations as $language => $translation) {
            $popup->setTranslation('title', $language, $translation['title']);
            $popup->setTranslation('description', $language, $translation['description']);
            $popup->setTranslation('popup_link', $language, $translation['popup_link']);
        }

        $popup->image = $this->image_path;
        $popup->image_mobile = $this->image_mobile_path;
        $popup->page_id = $this->page_id ? $this->page_id : null;
        $popup->display_type = $this->display_type;
        $popup->popup_display_scope = $this->popup_display_scope;
        $popup->popup_display_freq = $this->popup_display_freq;

        $popup->save();

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

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

        $this->dispatch('showToast', ['message' => 'Popup created successfully!']);
    }

    public function saveAndRedirect()
    {
        $this->save();
        return redirect()->route('admin.popup.index');
    }

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