<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Models\Artwork;
use App\Services\ArtworkService;
use App\Services\Maturity\ArtworkMaturityService;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Str;

class FeaturedArtworksController extends Controller
{
    protected ArtworkService $artworks;

    public function __construct(ArtworkService $artworks, private readonly ArtworkMaturityService $maturity)
    {
        $this->artworks = $artworks;
    }

    public function index(Request $request)
    {
        $perPage = 39;

        $type = (int) ($request->query('type', 4));

        $typeFilter = $type === 4 ? null : $type;

        /** @var LengthAwarePaginator $artworks */
        $artworks = $this->artworks->getFeaturedArtworks($typeFilter, $perPage);

        $artworks->setCollection(
            collect($this->maturity->filterPayloadItems($artworks->getCollection()->map(function (Artwork $artwork): array {
            $primaryCategory = $artwork->categories->sortBy('sort_order')->first();
            $categoryName = $primaryCategory->name ?? '';
            $categorySlug = $primaryCategory->slug ?? '';
            $gid = $primaryCategory ? ((int) $primaryCategory->id % 5) * 5 : 0;
            $present = \App\Services\ThumbnailPresenter::present($artwork, 'md');
            $username = $artwork->user->username ?? $artwork->user->name ?? 'Skinbase';

            return $this->maturity->decoratePayload([
                'id' => $artwork->id,
                'name' => $artwork->title,
                'slug' => $artwork->slug,
                'url' => url('/art/' . $artwork->id . '/' . Str::slug($artwork->title ?? 'artwork')),
                'content_type_name' => $primaryCategory?->contentType?->name ?? '',
                'content_type_slug' => $primaryCategory?->contentType?->slug ?? '',
                'category_name' => $categoryName,
                'category_slug' => $categorySlug,
                'gid_num' => $gid,
                'thumb_url' => $present['url'],
                'thumb_srcset' => $present['srcset'] ?? $present['url'],
                'width' => $artwork->width,
                'height' => $artwork->height,
                'uname' => $artwork->user->name ?? 'Skinbase',
                'username' => $username,
            ], $artwork, $request->user());
            })->values()->all(), $request->user()))
                ->map(static fn (array $item): object => (object) $item)
                ->values()
        );

        $artworkTypes = [
            1 => 'Bronze Awards',
            2 => 'Silver Awards',
            3 => 'Gold Awards',
            4 => 'Featured Artworks',
        ];

        $pageTitle = $artworkTypes[$type] ?? 'Featured Artworks';

        return view('web.featured-artworks', [
            'artworks' => $artworks,
            'type' => $type,
            'artworkTypes' => $artworkTypes,
            'page_title' => $pageTitle,
        ]);
    }
}
