<?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', 'min:10', 'max:160'],
            'meta_keywords' => ['required', 'string', 'min:3', 'max:255'],
            'meta_author' => ['required', 'string', 'min:1', 'max:150'],
            'og_image' => ['nullable', 'string', 'max:500', 'url'],
            'og_title' => ['nullable', 'string', 'max:255'],
            'og_description' => ['nullable', 'string', 'max:300'],
            'og_type' => ['nullable', 'string', 'max:50', 'in:website,article,product'],
            'twitter_card' => ['nullable', 'string', 'max:50', 'in:summary,summary_large_image'],
            'canonical_url' => ['nullable', 'string', 'max:500', 'url'],
        ];
    }

    public function validationAttributes(): array
    {
        return [
            'meta_description' => 'meta deskripsi',
            'meta_keywords' => 'meta kata kunci',
            'meta_author' => 'meta penulis',
            'og_image' => 'gambar Open Graph',
            'og_title' => 'judul Open Graph',
            'og_description' => 'deskripsi Open Graph',
            'og_type' => 'tipe Open Graph',
            'twitter_card' => 'kartu Twitter',
            'canonical_url' => 'URL kanonis',
        ];
    }

    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();
    }
}
