<?php

namespace App\Http\Controllers;

use App\Http\Requests\CompraStoreRequest;
use App\Http\Requests\CompraUpdateRequest;
use App\Http\Resources\CompraCollection;
use App\Http\Resources\CompraResource;
use App\Models\Compra;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class CompraController extends Controller
{
    public function index(Request $request): CompraCollection
    {
        $compras = Compra::all();

        return new CompraCollection($compras);
    }

    public function store(CompraStoreRequest $request): CompraResource
    {
        $compra = Compra::create($request->validated());

        return new CompraResource($compra);
    }

    public function show(Request $request, Compra $compra): CompraResource
    {
        return new CompraResource($compra);
    }

    public function update(CompraUpdateRequest $request, Compra $compra): CompraResource
    {
        $compra->update($request->validated());

        return new CompraResource($compra);
    }

    public function destroy(Request $request, Compra $compra): Response
    {
        $compra->delete();

        return response()->noContent();
    }
}
