<?php

namespace App\Http\Controllers\Legacy;

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

class DailyUploadsController extends Controller
{
    protected ArtworkService $artworks;

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

    public function index(Request $request)
    {
        $isAjax = $request->boolean('ajax');
        $datum = $request->query('datum');

        if ($isAjax && $datum) {
            // Return partial gallery for the given date
            $arts = $this->fetchByDate($datum);
            return view('legacy::partials.daily-uploads-grid', ['arts' => $arts])->render();
        }

        // Build date tabs (today .. -14 days)
        $dates = [];
        for ($x = 0; $x > -15; $x--) {
            $ts = strtotime(sprintf('%+d days', $x));
            $dates[] = [
                'iso' => date('Y-m-d', $ts),
                'label' => date('d. F Y', $ts),
            ];
        }

        // initial content: recent (last 7 days)
        $recent = $this->fetchRecent();

        return view('legacy::daily-uploads', [
            'dates' => $dates,
            'recent' => $recent,
            'page_title' => 'Daily Uploads',
        ]);
    }

    private function fetchByDate(string $date)
    {
        $ars = Artwork::public()
            ->published()
            ->whereDate('published_at', $date)
            ->orderByDesc('published_at')
            ->with(['user:id,name', 'categories' => function ($q) {
                $q->select('categories.id', 'categories.name', 'categories.sort_order');
            }])
            ->get();

        return $this->prepareArts($ars);
    }

    private function fetchRecent()
    {
        $start = now()->subDays(7)->startOfDay();

        $ars = Artwork::public()
            ->published()
            ->where('published_at', '>=', $start)
            ->orderByDesc('published_at')
            ->with(['user:id,name', 'categories' => function ($q) {
                $q->select('categories.id', 'categories.name', 'categories.sort_order');
            }])
            ->get();

        return $this->prepareArts($ars);
    }

    private function prepareArts($ars)
    {
        return $ars->map(function (Artwork $ar) {
            $primaryCategory = $ar->categories->sortBy('sort_order')->first();
            $present = \App\Services\ThumbnailPresenter::present($ar, 'md');

            return (object) [
                'id' => $ar->id,
                'name' => $ar->title,
                'thumb' => $present['url'],
                'thumb_srcset' => $present['srcset'] ?? $present['url'],
                'gid_num' => $primaryCategory ? ((int) $primaryCategory->id % 5) * 5 : 0,
                'category_name' => $primaryCategory->name ?? '',
                'uname' => $ar->user->name ?? 'Skinbase',
            ];
        });
    }
}
