<?php

namespace App\Livewire\Admin\Services;

use App\Models\Menu;
use App\Models\Service;
use App\Models\ServiceCategory;
use App\Models\Work;
use Illuminate\Database\Eloquent\Collection;
use Livewire\Component;

class ServiceCreate extends Component
{
    // ================= FORM FIELDS =================
    public $title;

    public $service_category_id;

    public $menu_id;

    public $description;

    public $hero_video;

    public $status = true;

    /** @var Collection */
    public $menus;

    public $selectedWorks = [];

    // ================= MOUNT =================
    public function mount()
    {
        // always empty collection initially
        $this->menus = collect();
    }

    // ================= VALIDATION =================
    protected function rules()
    {
        return [
            'service_category_id' => 'required|exists:service_categories,id',
            'menu_id' => 'required|exists:menus,id',
            'description' => 'nullable|string',
            'hero_video' => 'nullable|url',
            'status' => 'boolean',
            'selectedWorks' => 'array',
            'selectedWorks.*' => 'exists:works,id',
        ];
    }

    // ================= LOAD MENUS (EXPLICIT & RELIABLE) =================
    public function loadMenus()
    {
        $this->menu_id = null;

        if (! $this->service_category_id) {
            $this->menus = collect();

            return;
        }

        $this->menus = Menu::where(
            'menu_category_id',
            (int) $this->service_category_id
        )
            ->orderBy('position')
            ->get();
    }

    // ================= PAGE CHANGE (OPEN PAGE) =================
    public function updatedMenuId($value)
    {
        if (! $value) {
            return;
        }

        $menu = Menu::find($value);

        if ($menu && $menu->url) {
            $this->dispatch('open-page', url: url($menu->url));
        }
    }

    // ================= SAVE =================
    public function save()
    {
        $this->validate();

        $menu = Menu::findOrFail($this->menu_id);

        $service = Service::create([
            'title' => $menu->title,
            'slug' => $menu->slug,
            'service_category_id' => $this->service_category_id,
            'menu_id' => $this->menu_id,
            'description' => $this->description,
            'hero_video' => $this->hero_video,
            'status' => $this->status ? 1 : 0,
        ]);

        if (! empty($this->selectedWorks)) {
            $service->works()->sync($this->selectedWorks);
        }

        return redirect()
            ->route('admin.services')
            ->with('success', 'Service created successfully');
    }

    // ================= RENDER =================
    public function render()
    {
        return view('livewire.admin.services.create', [
            'categories' => ServiceCategory::where('status', 1)
                ->orderBy('name')
                ->get(),
            'works' => Work::orderBy('client_name')->get(),
        ])->layout('layouts.admin');
    }
}
