<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use App\Http\Resources\SliderResource;
use App\Models\Post;
use App\Models\Slider;

class SliderController extends Controller
{
    public function index()
    {
        $lang = request()->header('Accept-Language') ?? 'ru';
        $sliders = Post::where('language', $lang)->orderBy('position')->get();

        return SliderResource::collection($sliders);
    }

    public function show($slider_id)
    {
        $slider = Post::find($slider_id);

        if (!$slider) {
            return response()->json([
                'message' => 'Slider not found'
            ], 404);
        }

        $slider->views += 1;
        $slider->save();

        return response()->json([
            'data' => [
                'id' => $slider->id,
                'name' => $slider->name,
                'image' => $slider->getImage(),
                'description' => $slider->content,
                'views' => $slider->views,
                'created_at' => $slider->created_at->format('Y-m-d H:i:s'),
            ]
        ]);
    }
}
