<?php

namespace App\Livewire\Home;

use App\Models\Brand;
use App\Models\Category;
use App\Models\ChooseUs;
use App\Models\Customer;
use App\Models\Faq;
use App\Models\Perfume;
use App\Models\PerfumeFeature;
use App\Models\Testimonial;
use Illuminate\Contracts\View\View;
use Livewire\Attributes\Layout;
use Livewire\Component;
use Livewire\WithPagination;

#[Layout('components.layouts.home', [
    'title' => 'Beranda',
])]
class Index extends Component
{
    use WithPagination;

    public array $bestSellingPerfumes = [];

    public array $brands = [];

    public array $perfumeFeatures = [];

    public array $faqs = [];

    public array $whyChooseUs = [];

    public int $totalMembersCount = 0;

    public $latestMembers = [];

    public array $testimonials = [];

    public function mount(): void
    {
        $this->bestSellingPerfumes = Perfume::withSum(['items' => function ($query) {
            $query->whereHas('order', function ($q) {
                $q->paid();
            });
        }], 'quantity')
            ->orderByDesc('items_sum_quantity')
            ->take(2)
            ->get()
            ->map(function (Perfume $perfume) {
                $perfume->image = $perfume->getFirstMediaUrl('image') ? $perfume->getFirstMediaUrl('image') : asset('assets/images/logo.png');
                $perfume->concentration_label = $perfume->concentration->label();

                return $perfume;
            })
            ->filter(fn (Perfume $perfume) => $perfume->items_sum_quantity > 0)
            ->toArray();

        $this->brands = Brand::orderBy('sort_order')
            ->get()
            ->map(function (Brand $brand) {
                $brand->image = $brand->getFirstMediaUrl('image') ? $brand->getFirstMediaUrl('image') : asset('assets/images/dark-logo.png');

                return $brand;
            })
            ->toArray();

        $this->whyChooseUs = ChooseUs::orderBy('sort_order')->get()->toArray();

        $this->perfumeFeatures = PerfumeFeature::orderBy('sort_order')->get()->toArray();

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

        $this->totalMembersCount = Customer::count();

        $this->latestMembers = Customer::whereNotNull('user_id')
            ->inRandomOrder()
            ->take(3)
            ->get();

        $this->testimonials = Testimonial::with(['user', 'user.customer'])
            ->show()
            ->latest()
            ->take(5)
            ->get()
            ->toArray();
    }

    public function render(): View
    {
        return view('livewire.home.index', [
            'pageTitle' => 'Beranda',
            'categories' => Category::perfume()
                ->orderBy('sort_order')
                ->paginate(12)
                ->through(function (Category $category) {
                    $category->image = $category->getFirstMediaUrl('image')
                        ?: asset('assets/images/logo.png');

                    return $category;
                }),
        ]);
    }
}
