<?php

namespace App\Http\Controllers;

use App\Models\Adresse;
use App\Models\Chapelle;
use Illuminate\Http\Request;

class ChapelleController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $dataChapelle = Chapelle::with('adresse')->get();

        if ($dataChapelle->isEmpty()) {
            return response()->json(['message' => 'No data found in Database'], 404);
        }

        return [
            'size' => sizeof($dataChapelle),
            'data' => $dataChapelle
        ];
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $req)
    {

        $champs = $req->validate([
            'ville' => 'required|string|max:255',
            'commune' => 'required|string|max:255',
            'quartier' => 'required|string|max:255',
            'avenue' => 'string|max:255',
            'numero' => 'numeric|min:0|max:3899',
            'designation' => 'required|string|max:255',
            'code' => 'required|string|max:255',
            'coordonnees' => 'required|string'

        ]);
        $dataAdresse = Adresse::create($champs);
        $req->merge([
            'adresse_id' =>  $dataAdresse->id
        ]);

        $chapelle = Chapelle::create($req->all());

        return $chapelle;
    }

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

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $req, $id)
    {
        $champs = $req->validate([
            'ville' => 'required|string|max:255',
            'commune' => 'required|string|max:255',
            'quartier' => 'required|string|max:255',
            'avenue' => 'string|max:255',
            'numero' => 'numeric|min:0|max:3899',
            'designation' => 'required|string|max:255',
            'code' => 'required|string|max:255',
            'coordonnees' => 'required|string'

        ]);
        $chapelleData = Chapelle::find($id);

        if (!$chapelleData) {
            return response()->json(['message' => 'Chapelle not found'], 404);
        }
        $adresseData = Adresse::find($chapelleData->adresse_id);
        $adresseData->update($champs);
        $chapelleData->Update($champs);

        return $chapelleData;
    }

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

        $adresse = Adresse::find($chapelle->adresse_id);
        $chapelle->delete();
        $adresse->delete();

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