<?php

namespace App\Livewire\Home;

use App\Models\Brand;
use App\Models\ChooseUs;
use App\Models\Faq;
use App\Models\PerfumeFeature;
use Illuminate\Support\Facades\Storage;
use Livewire\Attributes\Layout;
use Livewire\Component;

#[Layout('components.layouts.home', [
    'title' => 'Beranda',
])]
class Index extends Component
{
    public array $brands = [];

    public array $perfumeFeatures = [];

    public array $faqs = [];

    public array $whyChooseUs = [];

    public function mount()
    {
        $this->brands = Brand::orderBy('sort_order')
            ->get()
            ->map(fn (Brand $brand) => [
                'name' => $brand->name,
                'image' => $brand->getFirstMediaUrl('image') ? Storage::url($brand->getFirstMediaUrl('image')) : asset('assets/images/logo.png'),
            ])
            ->toArray();

        $this->perfumeFeatures = PerfumeFeature::orderBy('sort_order')
            ->get()
            ->map(fn (PerfumeFeature $perfumeFeature) => [
                'name' => $perfumeFeature->name,
                'description' => $perfumeFeature->description,
            ])
            ->toArray();

        $this->faqs = Faq::orderBy('sort_order')
            ->get()
            ->map(fn (Faq $faq) => [
                'question' => $faq->question,
                'answer' => $faq->answer,
            ])
            ->toArray();

        $this->whyChooseUs = ChooseUs::orderBy('sort_order')
            ->get()
            ->map(fn (ChooseUs $chooseUs) => [
                'name' => $chooseUs->name,
                'description' => $chooseUs->description,
            ])
            ->toArray();
    }

    public function render()
    {
        return view('livewire.home.index', [
            'pageTitle' => 'Beranda',
        ]);
    }
}
