<?php

declare(strict_types=1);

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Http\Resources\ArtworkListResource;
use App\Services\ArtworkSearchService;
use App\Services\GroupDiscoveryService;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Http\Request;
use Illuminate\View\View;
use cPad\Plugins\News\Models\NewsArticle;

final class SearchController extends Controller
{
    public function __construct(
        private readonly ArtworkSearchService $search,
        private readonly GroupDiscoveryService $groups,
    ) {}

    public function index(Request $request): View
    {
        $q    = trim((string) $request->query('q', ''));
        $sort = $request->query('sort', 'latest');
        $hasQuery = $q !== '';

        $sortMap = [
            'popular'   => 'views:desc',
            'likes'     => 'likes:desc',
            'latest'    => 'created_at:desc',
            'downloads' => 'downloads:desc',
        ];

        $artworks = $hasQuery
            ? $this->search->search($q, [
                'sort' => ($sortMap[$sort] ?? 'created_at:desc'),
            ])
            : $this->search->popular(24);

        $groups = $hasQuery
            ? $this->groups->searchCards($q, $request->user(), 6)
            : $this->groups->surfaceCards($request->user(), 'featured', 4);

        $news = $hasQuery
            ? NewsArticle::query()
                ->with(['author:id,username,name', 'category:id,name,slug'])
                ->published()
                ->where(function ($builder) use ($q): void {
                    $builder->where('title', 'like', '%' . $q . '%')
                        ->orWhere('excerpt', 'like', '%' . $q . '%')
                        ->orWhere('content', 'like', '%' . $q . '%')
                        ->orWhere('meta_title', 'like', '%' . $q . '%');
                })
                ->editorialOrder()
                ->limit(4)
                ->get()
            : collect();

        $groupResults = collect($groups ?? []);
        $newsResults = collect($news ?? []);
        $resultCount = method_exists($artworks, 'total') ? (int) $artworks->total() : 0;
        $groupResultCount = $groupResults->count();
        $newsResultCount = $newsResults->count();
        $hasAnyResults = $resultCount > 0 || $groupResultCount > 0 || $newsResultCount > 0;

        $galleryItems = method_exists($artworks, 'getCollection')
            ? $artworks->getCollection()
            : new EloquentCollection(collect($artworks)->all());

        $galleryItems->loadMissing(['user.profile', 'group', 'categories.contentType']);

        $galleryArtworks = $galleryItems
            ->map(fn ($artwork) => (new ArtworkListResource($artwork))->resolve($request))
            ->values()
            ->all();

        $galleryNextPageUrl = method_exists($artworks, 'nextPageUrl') ? $artworks->nextPageUrl() : null;

        return view('search.index', [
            'q'                     => $q,
            'hasQuery'              => $hasQuery,
            'sort'                  => $sort,
            'groups'                => $groups,
            'groupResults'          => $groupResults,
            'groupResultCount'      => $groupResultCount,
            'artworks'              => $artworks,
            'resultCount'           => $resultCount,
            'news'                  => $news,
            'newsResults'           => $newsResults,
            'newsResultCount'       => $newsResultCount,
            'hasAnyResults'         => $hasAnyResults,
            'galleryArtworks'       => $galleryArtworks,
            'galleryNextPageUrl'    => $galleryNextPageUrl,
            'page_title'            => $hasQuery ? 'Search: ' . $q . ' — Skinbase' : 'Search — Skinbase',
            'page_meta_description' => 'Search Skinbase for artworks, creators, groups, photography, wallpapers and skins.',
            'page_robots'           => 'noindex,follow',
        ]);
    }
}
