<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;


use Illuminate\Database\Eloquent\Model;

use App\Models\ManageState;
use App\Models\IamPrincipalRestaurantRole;



class ManageRestaurant extends Model
{
    use SoftDeletes;
    use HasFactory;
    protected $table = 'manage_restaurants';
    protected $fillable = [
        'short_id', 'name', 'description', 'restaurant_id', 'address', 'image', 'bio',
        'try_on_1', 'try_on_2', 'try_on_3', 'try_on_4', 'exclusion', 'latitude', 'longitude',
        'is_active', 'created_by', 'modified_by', 'phone_number'
    ];

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            if (empty($model->short_id)) {
                $model->short_id = static::generateUniqueShortId();
            }
        });
    }

    protected static function generateUniqueShortId()
    {
        do {
            $shortId = Str::random(4);
        } while (static::where('short_id', $shortId)->exists());

        return $shortId;
    }
    public function operatingHours()
    {
        return $this->hasMany(OperatingHour::class,'manage_restaurant_xid', 'id');
    }

    public function state()
    {
        return $this->belongsTo(ManageState::class, 'state_xid', 'id');
    }



}
