<?php

namespace App\Http\Controllers;

use App\Models\Ingatlan;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class IngatlanController extends Controller
{
    public function index(): JsonResponse{
        return response()->json(Ingatlan::getAllWithCategory(), 200);
    }

    public function update(Request $request, $id){
        $validate = $request->validate([
            'kategoria' => 'required|integer|exists:kategoriak,id',
            'leiras' => 'required|string',
            'hirdetesDatuma' => 'required|date',
            'tehermentes' => 'required|boolean',
            'ar' => 'required|integer|min:0',
            'kepUrl' => 'required|url'
        ]);

        $ingatlan = Ingatlan::updateIngatlan($id, $validate);

        if(!$ingatlan){
            return response()->json(['message' => 'Ingatlan nem létezik'], 404);
        }

        return response()->json([
            'id' => $ingatlan->id,
            'kategoria' => $ingatlan->kategoria->nev ?? null,
            'leiras' => $ingatlan->leiras,
            'ar' => $ingatlan->ar,
            'hirdetesDatuma' => $ingatlan->hirdetesDatuma,
            'tehermentes' => $ingatlan->tehermentes,
            'kepurl' => $ingatlan->kepUrl
        ],200);


    }
}
