<?php

namespace Modules\CarFeature\Http\Controllers;

use App\Functions\ResponseHelper;
use App\Http\Controllers\BasicController;
use Modules\Car\Entities\Model as Car;
use Modules\Car\Http\Resources\Resource;
use Modules\Car\Http\Requests\StoreRequest;
use Modules\Car\Http\Requests\UpdateRequest;

class APIController extends BasicController
{
    public function index()
    {
        $cars = Car::Active()->get();
        $data = Resource::collection($cars);

        return ResponseHelper::make($data);
    }
    

    public function store(StoreRequest $request)
    {
        $car = Car::create($request->validated());
        return response()->json($car, 201);
    }

    public function show($id)
    {
        return Car::with(['features', 'images', 'offers'])->findOrFail($id);
    }

    public function update(UpdateRequest $request, $id)
    {
        $car = Car::findOrFail($id);
        $car->update($request->validated());
        return response()->json($car);
    }

    public function destroy($id)
    {
        Car::findOrFail($id)->delete();
        return response()->json(['message' => 'Car deleted successfully']);
    }
}
