<?php

namespace App\Http\Controllers;

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


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

        return [
            'size' => sizeof($dataCarteSous),
            'data' => $dataCarteSous
        ];
    }
    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $req)
    {
        $champs = $req->validate([
            'annee' => 'required|string|max:255',
            'montant' => 'required|numeric',
            'situation' => 'required|numeric',
            'personne_id' => 'required|exists:personnes,id',
            'service_id' => 'required|exists:services,id',
            'devise_id' => 'required|exists:devises,id'
        ]);

        $dataCarteSous = Carte_souscription::create($champs);
        return $dataCarteSous;
    }

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

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $req, $id)
    {
        $dataCarteSous = Carte_souscription::find($id);
        if (!$dataCarteSous) {
            return response()->json(['message' => 'Carte_souscription not found'], 404);
        }
        $champs = $req->validate([
            'annee' => 'required|string|max:255',
            'montant' => 'required|numeric',
            'situation' => 'required|string|string',
            'personne_id' => 'required|exists:personnes,id',
            'service_id' => 'required|exists:services,id',
            'devise_id' => 'required|exists:devises,id'
        ]);
        $dataCarteSous->update($champs);
        return $dataCarteSous;
    }

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

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