<?php

namespace App\Http\Controllers;

use App\Models\Vente_ouvrage;
use Illuminate\Http\Request;


class VenteOuvrageController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $dataRespoService = Vente_ouvrage::all();
        if ($dataRespoService->isEmpty()) {
            return response()->json(['message' => 'No Vente_ouvrage data found in Database'], 404);
        }

        return [
            'size' => sizeof($dataRespoService),
            'data' => $dataRespoService
        ];
    }
    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $req)
    {
        $champs = $req->validate([
            'quantite' => 'required|numeric',
            'motif' => 'required|string|max:255',
            'personne_id' => 'required|exists:personnes,id',
            'ouvrage_id' => 'required|exists:ouvrage_livres,id'
        ]);

        $dataRespoService = Vente_ouvrage::create($champs);
        return $dataRespoService;
    }

    /**
     * Display the specified resource.
     */
    public function show($id)
    {
        $Vente_ouvrage = Vente_ouvrage::find($id);
        if (!$Vente_ouvrage) {
            return response()->json(['message' => 'Vente_ouvrage not found'], 404);
        }
        return response()->json($Vente_ouvrage);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $req, $id)
    {
        $ParenteData = Vente_ouvrage::find($id);
        if (!$ParenteData) {
            return response()->json(['message' => 'Vente_ouvrage not found'], 404);
        }
        $champs = $req->validate([
            'quantite' => 'required|numeric',
            'motif' => 'required|string|max:255',
            'personne_id' => 'required|exists:personnes,id',
            'ouvrage_id' => 'required|exists:ouvrage_livres,id'
        ]);
        $ParenteData->update($champs);
        return $ParenteData;
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id)
    {
        $ParenteData = Vente_ouvrage::find($id);
        if (!$ParenteData) {
            return response()->json(['message' => 'Vente_ouvrage not found'], 404);
        }
        $ParenteData->delete();

        return [
            'message' => "Vente_ouvrage deleted"
        ];
    }
}