<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Ingatlan extends Model
{
    //use HashFactory;
    protected $table='ingatlanok';
    protected $fillable = ['kategoria', 'leiras', 'hirdetesDatuma', 'tehermentes', 'ar', 'kepUrl'] ;

    public $timestamps = false;

    public function kategoriak(){
        return $this->belongsTo(Kategoria::class, 'kategoria');
    }

    public static function getAllWithCategory(){
        return self::with('kategoriak:id,nev')->get()->map(function ($ingatlan){
            return [
                'id' => $ingatlan->id,
                'kategoria' => $ingatlan->kategoriak->nev ?? null,
                'leiras' => $ingatlan->leiras,
                'hirdetesDatuma' => $ingatlan->hirdetesDatuma,
                'tehermentes' => $ingatlan->tehermentes,
                'ar' => $ingatlan->ar,
                'kepUrl' => $ingatlan->kepUrl,
            ];
        });
    }

    public static function updateIngatlan($id, $data){
        $ingatlan = self::find($id);
        if  (!ingatlan) return null;

        $ingatlan->update($data);
    }

}
