<?php

namespace App\Http\Controllers\Dashboard;

use App\Http\Controllers\Controller;
use App\Models\Artwork;
use Illuminate\Http\Request;
use Illuminate\View\View;

class DashboardAwardsController extends Controller
{
    public function index(Request $request): View
    {
        $user = $request->user();

        $artworks = Artwork::query()
            ->where('user_id', (int) $user->id)
            ->whereHas('awards')
            ->with(['awardStat', 'stats', 'categories.contentType'])
            ->orderByDesc(
                \App\Models\ArtworkAwardStat::select('score_total')
                    ->whereColumn('artwork_id', 'artworks.id')
                    ->limit(1)
            )
            ->paginate(24)
            ->withQueryString();

        return view('dashboard.awards', [
            'artworks'  => $artworks,
            'page_title' => 'My Awards – SkinBase',
        ]);
    }
}
