<?php
namespace App\Http\Controllers\Api\Admin;
use App\Http\Controllers\Controller;
use App\Models\Banner;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class BannerController extends Controller
{
    public function index() { return response()->json(['success'=>true,'data'=>Banner::orderBy('sort_order')->get()]); }
    public function store(Request $request)
    {
        $request->validate(['title'=>'required','image'=>'required|image|max:2048','type'=>'required|in:hero,promo','link'=>'nullable|url']);
        $path = Storage::url($request->file('image')->store('banners','public'));
        return response()->json(['success'=>true,'data'=>Banner::create(['title'=>$request->title,'image'=>$path,'type'=>$request->type,'link'=>$request->link,'is_active'=>true])],201);
    }
    public function update(Request $request, int $id) { Banner::findOrFail($id)->update($request->only('title','link','sort_order','is_active')); return response()->json(['success'=>true]); }
    public function destroy(int $id) { Banner::findOrFail($id)->delete(); return response()->json(['success'=>true]); }
}
