<?php

namespace Modules\Admin\App\Livewire\WebsiteSettings;

use Livewire\Component;
use App\Services\WebsiteTemplateService;
use Modules\Admin\App\Services\WebsiteSettingsService;

class SocialSettings extends Component
{
    private $targetNotify = "#website-social-settings-card .notification-container";

    public $social_whatsapp,
        $social_whatsapp_message,
        $social_facebook,
        $social_instagram,
        $social_linkedin,
        $social_tiktok,
        $social_x_twitter,
        $social_google,
        $social_pinterest,
        $social_youtube,
        $social_vimeo;

    protected $listeners = ['saveSocialSettings' => 'save'];


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

    public function loadSettings()
    {
        $websiteTemplateService = app(WebsiteTemplateService::class);

        // Obtener los valores de las configuraciones de la base de datos
        $settings = $websiteTemplateService->getSocialVars();

        $this->social_whatsapp  = $settings['whatsapp'];
        $this->social_whatsapp_message = $settings['whatsapp_message'];
        $this->social_facebook  = $settings['facebook'];
        $this->social_instagram = $settings['instagram'];
        $this->social_linkedin  = $settings['linkedin'];
        $this->social_tiktok    = $settings['tiktok'];
        $this->social_x_twitter = $settings['x_twitter'];
        $this->social_google    = $settings['google'];
        $this->social_pinterest = $settings['pinterest'];
        $this->social_youtube   = $settings['youtube'];
        $this->social_vimeo     = $settings['vimeo'];
    }

    public function save()
    {
        $this->validate([
            'social_whatsapp' => 'string|max:20',
            'social_whatsapp_message' => 'string|max:255',
            'social_facebook' => 'url',
            'social_instagram' => 'url',
            'social_linkedin' => 'url',
            'social_tiktok' => 'url',
            'social_x_twitter' => 'url',
            'social_google' => 'url',
            'social_pinterest' => 'url',
            'social_youtube' => 'url',
            'social_vimeo' => 'url',
        ]);

        $websiteSettingsService = app(websiteSettingsService::class);

        // Guardar título del App en configuraciones
        $websiteSettingsService->updateSetting('social_whatsapp', preg_replace('/\D/', '', $this->social_whatsapp));
        $websiteSettingsService->updateSetting('social_whatsapp_message', $this->social_whatsapp_message);
        $websiteSettingsService->updateSetting('social_facebook', $this->social_facebook);
        $websiteSettingsService->updateSetting('social_instagram', $this->social_instagram);
        $websiteSettingsService->updateSetting('social_linkedin', $this->social_linkedin);
        $websiteSettingsService->updateSetting('social_tiktok', $this->social_tiktok);
        $websiteSettingsService->updateSetting('social_x_twitter', $this->social_x_twitter);
        $websiteSettingsService->updateSetting('social_google', $this->social_google);
        $websiteSettingsService->updateSetting('social_pinterest', $this->social_pinterest);
        $websiteSettingsService->updateSetting('social_youtube', $this->social_youtube);
        $websiteSettingsService->updateSetting('social_vimeo', $this->social_vimeo);

        app(WebsiteTemplateService::class)->clearWebsiteVarsCache();

        $this->loadSettings();

        $this->dispatch(
            'notification',
            target: $this->targetNotify,
            type: 'success',
            message: 'Se han guardado los cambios en las configuraciones.'
        );
    }

    public function render()
    {
        return view('admin::livewire.website-settings.social-settings');
    }
}
