<?php

namespace App\Livewire\Admin\Services;

use App\Models\ServiceCategory;
use Illuminate\Support\Str;
use Livewire\Component;

class ServiceCategoryEdit extends Component
{
    public $categoryId;
    public $name;
    public $description;
    public $h1;
    public $h2;
    public $video_url;
    public $slug;
    public $status = true;


    // ================= SEO FIELDS =================
    public $meta_title;
    public $meta_description;
    public $meta_keywords;
    public $canonical_url;
    public $og_title;
    public $og_description;
    public $og_image;

    // ================= ROBOTS =================
    public $noindex = false;
    public $nofollow = false;

    // ================= SCHEMA =================
    public $schema_type = 'CollectionPage';
    public $schema_markup;
    public $faq_schema;
    public $org_schema;
    public $webpage_schema;
    public $breadcrumb_schema;
    public $localbusiness_schema;

    public function mount($id)
    {
        $category = ServiceCategory::findOrFail($id);

        // ================= BASIC =================
        $this->categoryId  = $category->id;
        $this->name        = $category->name;
        $this->description = $category->description;
        $this->h1 = $category->h1;
        $this->h2 = $category->h2;
        $this->video_url   = $category->video_url;
        $this->slug        = $category->slug;
        $this->status = (bool) $category->status;


        // ================= SEO =================
        $this->meta_title = $category->meta_title;
        $this->meta_description = $category->meta_description;
        $this->meta_keywords = $category->meta_keywords;
        $this->canonical_url = $category->canonical_url;
        $this->og_title = $category->og_title;
        $this->og_description = $category->og_description;
        $this->og_image = $category->og_image;

        // ================= ROBOTS =================
        $this->noindex = (bool) $category->noindex;
        $this->nofollow = (bool) $category->nofollow;

        // ================= SCHEMA =================
        $this->schema_type = $category->schema_type ?? 'CollectionPage';
        $this->schema_markup = $category->schema_markup;
        $this->faq_schema = $category->faq_schema;
        $this->org_schema = $category->org_schema;
        $this->webpage_schema = $category->webpage_schema;
        $this->breadcrumb_schema = $category->breadcrumb_schema;
        $this->localbusiness_schema = $category->localbusiness_schema;
    }

    protected function rules()
    {
        return [
            // BASIC
            'name'        => 'required|string|max:255',
            'description' => 'nullable|string|max:500',
            'h1' => 'nullable|string|max:1000',
            'h2' => 'nullable|string|max:1000',
            'video_url'   => 'nullable|url',
            'slug'        => 'required|alpha_dash|unique:service_categories,slug,' . $this->categoryId,
            'status'      => 'required|boolean',

            // SEO
            'meta_title' => 'nullable|string|max:255',
            'meta_description' => 'nullable|string|max:255',
            'meta_keywords' => 'nullable|string|max:1000',
            'canonical_url' => 'nullable|url|max:255',
            'og_title' => 'nullable|string|max:255',
            'og_description' => 'nullable|string|max:255',
            'og_image' => 'nullable|url|max:255',

            // ROBOTS
            'noindex' => 'boolean',
            'nofollow' => 'boolean',

            // SCHEMA
            'schema_type' => 'nullable|string|max:50',
            'schema_markup' => 'nullable|string',
            'faq_schema' => 'nullable|string',
            'org_schema' => 'nullable|string',
            'webpage_schema' => 'nullable|string',
            'breadcrumb_schema' => 'nullable|string',
            'localbusiness_schema' => 'nullable|string',
        ];
    }

    public function updatedName()
    {
        $this->slug = Str::slug($this->name);

        // Auto-fill SEO title if empty
        if (empty($this->meta_title)) {
            $this->meta_title = $this->name;
        }

        // Auto-fill OG title if empty
        if (empty($this->og_title)) {
            $this->og_title = $this->name;
        }
    }

    public function update()
    {
        $this->validate();

        ServiceCategory::where('id', $this->categoryId)->update([

            // BASIC
            'name'        => $this->name,
            'description' => $this->description,
            'h1' => $this->h1,
            'h2' => $this->h2,
            'video_url'   => $this->video_url,
            'slug'        => $this->slug,
            'status'      => $this->status,

            // SEO
            'meta_title' => $this->meta_title ?: $this->name,
            'meta_description' => $this->meta_description,
            'meta_keywords' => $this->meta_keywords,
            'canonical_url' => $this->canonical_url,
            'og_title' => $this->og_title ?: $this->name,
            'og_description' => $this->og_description ?: $this->meta_description,
            'og_image' => $this->og_image,

            // ROBOTS
            'noindex' => $this->noindex ? 1 : 0,
            'nofollow' => $this->nofollow ? 1 : 0,

            // SCHEMA
            'schema_type' => $this->schema_type,
            'schema_markup' => $this->schema_markup,
            'faq_schema' => $this->faq_schema,
            'org_schema' => $this->org_schema,
            'webpage_schema' => $this->webpage_schema,
            'breadcrumb_schema' => $this->breadcrumb_schema,
            'localbusiness_schema' => $this->localbusiness_schema,
        ]);

        session()->flash('success', 'Category updated successfully');

        return redirect()->route('admin.services');
    }

    public function render()
    {
        return view('livewire.admin.services.category-edit')
            ->layout('layouts.admin');
    }
}