<?php

namespace App\Livewire\Studio\Setting;

use App\Livewire\Forms\Studio\Setting\ContactForm;
use App\Livewire\Forms\Studio\Setting\GeneralForm;
use App\Livewire\Forms\Studio\Setting\SeoForm;
use App\Livewire\Forms\Studio\Setting\SocialForm;
use App\Settings\ContactSettings;
use App\Settings\GeneralSettings;
use App\Settings\SeoSettings;
use App\Settings\SocialSettings;
use App\Traits\WithAuthorization;
use App\Traits\WithToast;
use App\Traits\WithUpdatedData;
use Livewire\Attributes\Title;
use Livewire\Component;

#[Title('Aplikasi')]
class Application extends Component
{
    use WithAuthorization, WithToast, WithUpdatedData;

    public GeneralForm $generalForm;

    public ContactForm $contactForm;

    public SocialForm $socialForm;

    public SeoForm $seoForm;

    public function mount(
        GeneralSettings $generalSettings,
        ContactSettings $contactSettings,
        SocialSettings $socialSettings,
        SeoSettings $seoSettings,
    ) {
        $this->generalForm->setSettings($generalSettings);

        $this->contactForm->setSettings($contactSettings);

        $this->socialForm->setSettings($socialSettings);

        $this->seoForm->setSettings($seoSettings);
    }

    public function updateGeneral(GeneralSettings $generalSettings)
    {
        $this->canOrAbort('update general settings');

        $this->generalForm->update($generalSettings);

        $this->toast('Pengaturan umum berhasil diperbarui.');
    }

    public function updateContact(ContactSettings $contactSettings)
    {
        $this->canOrAbort('update contact settings');

        $this->contactForm->update($contactSettings);

        $this->toast('Pengaturan kontak berhasil diperbarui.');
    }

    public function updateSocial(SocialSettings $socialSettings)
    {
        $this->canOrAbort('update social settings');

        $this->socialForm->update($socialSettings);

        $this->toast('Pengaturan media sosial berhasil diperbarui.');
    }

    public function updateSeo(SeoSettings $seoSettings)
    {
        $this->canOrAbort('update seo settings');

        $this->seoForm->update($seoSettings);

        $this->toast('Pengaturan SEO berhasil diperbarui.');
    }

    public function render()
    {
        return view('livewire.studio.setting.application', [
            'pageTitle' => 'Aplikasi',
        ]);
    }
}
