<?php

namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\Product;
use App\Services\S3PresignedService;
use App\Settings\HomepageSettings;
use App\Settings\SocialMediaSettings;
use App\Settings\SystemSettings;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class HomepageController extends Controller
{
    public function __construct(
        private S3PresignedService $s3Service,
    ) {}

    public function __invoke(Request $request): Response
    {
        $system = app(SystemSettings::class);
        $homepage = app(HomepageSettings::class);
        $socialMedia = app(SocialMediaSettings::class);

        $categories = Category::select(['id', 'name', 'slug'])
            ->orderBy('name')
            ->get();

        $search = $request->input('search', '');
        $category = $request->input('category', '');

        $productsQuery = Product::select(['id', 'name', 'slug', 'description', 'status'])
            ->active()
            ->with([
                'categories:id,name,slug',
                'productVariants:id,product_id,name,stock',
                'productVariants.productPrices:id,variant_id,type,price',
            ]);

        if ($search !== '') {
            $productsQuery->where(function ($q) use ($search) {
                $q->where('name', 'like', "%{$search}%")
                    ->orWhere('description', 'like', "%{$search}%");
            });
        }

        if ($category !== '') {
            $productsQuery->whereHas('categories', function ($q) use ($category) {
                $q->where('slug', $category);
            });
        }

        $products = Inertia::scroll(
            fn () => $productsQuery->orderBy('created_at', 'desc')->paginate(12)
        );

        $galleryImages = array_map(
            fn ($key) => str_starts_with($key, 'http') ? $key : $this->s3Service->getTemporaryUrl($key, 60),
            $homepage->gallery_images ?? [],
        );

        return Inertia::render('welcome', [
            'appName' => $system->app_name,
            'aboutApp' => $system->about_app,
            'contactEmail' => $system->email,
            'contactPhone' => $system->phone,
            'contactAddress' => $system->address,
            'instagramUrl' => $socialMedia->instagram_url,
            'facebookUrl' => $socialMedia->facebook_url,
            'tiktokUrl' => $socialMedia->tiktok_url,
            'homepage' => [
                'hero_badge' => $homepage->hero_badge,
                'hero_title_line1' => $homepage->hero_title_line1,
                'hero_title_line2' => $homepage->hero_title_line2,
                'hero_title_highlight' => $homepage->hero_title_highlight,
                'hero_description' => $homepage->hero_description,
                'hero_cta_primary_text' => $homepage->hero_cta_primary_text,
                'hero_cta_secondary_text' => $homepage->hero_cta_secondary_text,
                'hero_image_url' => $homepage->hero_image_url
                    ? (str_starts_with($homepage->hero_image_url, 'http') ? $homepage->hero_image_url : $this->s3Service->getTemporaryUrl($homepage->hero_image_url, 60))
                    : null,
                'hero_bg_text_left' => $homepage->hero_bg_text_left,
                'hero_bg_text_right' => $homepage->hero_bg_text_right,
                'scroll_hashtag' => $homepage->scroll_hashtag,
                'scroll_tagline' => $homepage->scroll_tagline,
                'catalog_badge' => $homepage->catalog_badge,
                'catalog_title' => $homepage->catalog_title,
                'catalog_description' => $homepage->catalog_description,
                'catalog_search_placeholder' => $homepage->catalog_search_placeholder,
                'gallery_badge' => $homepage->gallery_badge,
                'gallery_title' => $homepage->gallery_title,
                'gallery_description' => $homepage->gallery_description,
                'gallery_images' => $galleryImages,
                'order_guide_badge' => $homepage->order_guide_badge,
                'order_guide_title' => $homepage->order_guide_title,
                'order_guide_description' => $homepage->order_guide_description,
                'order_steps' => $homepage->order_steps,
                'about_badge' => $homepage->about_badge,
                'about_title' => $homepage->about_title,
                'about_image_url' => $homepage->about_image_url
                    ? (str_starts_with($homepage->about_image_url, 'http') ? $homepage->about_image_url : $this->s3Service->getTemporaryUrl($homepage->about_image_url, 60))
                    : null,
                'about_features' => $homepage->about_features,
                'contact_badge' => $homepage->contact_badge,
                'contact_title' => $homepage->contact_title,
                'contact_description' => $homepage->contact_description,
                'contact_form_title' => $homepage->contact_form_title,
                'footer_description' => $homepage->footer_description,
                'footer_copyright' => $homepage->footer_copyright,
            ],
            'categories' => $categories,
            'products' => $products,
            'filters' => [
                'search' => $search,
                'category' => $category,
            ],
            'seo' => [
                'title' => $system->app_name.' - '.$homepage->hero_badge,
                'description' => $homepage->hero_description,
                'image' => url('/assets/logo.png'),
                'url' => url('/'),
            ],
        ]);
    }
}
