<?php

namespace App\Livewire\Forms\Studio\Setting;

use App\Settings\SeoSettings;
use Livewire\Form;

class SeoForm extends Form
{
    public string $meta_description = '';

    public string $meta_keywords = '';

    public string $meta_author = '';

    public ?string $og_image = null;

    public ?string $og_title = null;

    public ?string $og_description = null;

    public ?string $og_type = null;

    public ?string $twitter_card = null;

    public ?string $canonical_url = null;

    public function rules(): array
    {
        return [
            'meta_description' => ['required', 'string'],
            'meta_keywords' => ['required', 'string'],
            'meta_author' => ['required', 'string', 'max:150'],
            'og_image' => ['nullable', 'string', 'max:255'],
            'og_title' => ['nullable', 'string', 'max:255'],
            'og_description' => ['nullable', 'string'],
            'og_type' => ['nullable', 'string', 'max:50'],
            'twitter_card' => ['nullable', 'string', 'max:50'],
            'canonical_url' => ['nullable', 'string', 'max:255'],
        ];
    }

    public function setSettings(SeoSettings $settings): void
    {
        $this->meta_description = $settings->meta_description;
        $this->meta_keywords = $settings->meta_keywords;
        $this->meta_author = $settings->meta_author;
        $this->og_image = $settings->og_image;
        $this->og_title = $settings->og_title;
        $this->og_description = $settings->og_description;
        $this->og_type = $settings->og_type;
        $this->twitter_card = $settings->twitter_card;
        $this->canonical_url = $settings->canonical_url;
    }

    public function update(SeoSettings $settings): void
    {
        $this->validate();

        $settings->meta_description = $this->meta_description;
        $settings->meta_keywords = $this->meta_keywords;
        $settings->meta_author = $this->meta_author;
        $settings->og_image = $this->og_image;
        $settings->og_title = $this->og_title;
        $settings->og_description = $this->og_description;
        $settings->og_type = $this->og_type;
        $settings->twitter_card = $this->twitter_card;
        $settings->canonical_url = $this->canonical_url;

        $settings->save();
    }
}
