<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Models\Artwork;
use App\Services\ArtworkService;
use Illuminate\Http\Request;

class MembersController extends Controller
{
    protected ArtworkService $artworks;

    public function __construct(ArtworkService $artworks)
    {
        $this->artworks = $artworks;
    }

    public function photos(Request $request, $id = null)
    {
        $artworks = $this->artworks->getArtworksByContentType('photography', 40);

        $artworks->getCollection()->load([
            'user:id,name,username',
            'user.profile:user_id,avatar_hash',
        ]);

        $artworks->getCollection()->transform(function (Artwork $artwork) {
            $primaryCategory = $artwork->categories->sortBy('sort_order')->first();
            $present = \App\Services\ThumbnailPresenter::present($artwork, 'md');

            return (object) [
                'id' => $artwork->id,
                'name' => $artwork->title,
                'slug' => $artwork->slug,
                'url' => '/art/' . $artwork->id . '/' . $artwork->slug,
                'thumb' => $present['url'],
                'thumb_url' => $present['url'],
                'thumb_srcset' => $present['srcset'] ?? $present['url'],
                'uname' => $artwork->user->name ?? 'Skinbase',
                'username' => $artwork->user->username ?? '',
                'avatar_url' => \App\Support\AvatarUrl::forUser((int) ($artwork->user->id ?? 0), $artwork->user->profile->avatar_hash ?? null, 64),
                'content_type_name' => $primaryCategory?->contentType?->name ?? 'Photography',
                'content_type_slug' => $primaryCategory?->contentType?->slug ?? 'photography',
                'category_name' => $primaryCategory?->name ?? '',
                'category_slug' => $primaryCategory?->slug ?? '',
                'width' => $artwork->width,
                'height' => $artwork->height,
                'published_at' => $artwork->published_at,
            ];
        });

        $page_title = 'Member Photos';

        return view('web.members.photos', compact('page_title', 'artworks'));
    }
}
