<?php

namespace App\Http\Controllers;

use App\Models\Image;
use App\Settings\GeneralSettings;
use Carbon\Carbon;
use Illuminate\Support\Facades\Lang;
use Inertia\Inertia;

class HomeController extends Controller
{
    public function __construct(private GeneralSettings $settings) {}

    public function index()
    {
        $locale = app()->getLocale();
        $translations = array_merge(
            Lang::get('api', [], $locale),
            Lang::get('settings', [], $locale)
        );
        $galleryHeading = $this->settings->gallery_heading;
        $newImageTimespanMinutes = $this->settings->new_image_timespan_minutes;

        $images = Image::all()->map(function ($image) use ($newImageTimespanMinutes) {
            $image->is_new = Carbon::parse($image->created_at)->diffInMinutes(Carbon::now()) <= $newImageTimespanMinutes;
            $image->path = 'storage/'.$image->path;

            return $image;
        });

        return Inertia::render('Home', [
            'translations' => $translations,
            'galleryHeading' => $galleryHeading,
            'images' => $images,
        ]);
    }
}
