<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\SoftDeletes;

class OrderSurvey extends BaseModel
{
    use HasFactory, SoftDeletes;

    protected function content(): Attribute
    {
        return Attribute::make(
            get: fn ($value) => json_decode($value, true),
        );
    }
    public function order()
    {
        return $this->belongsTo(Order::class);
    }

    public function scopeKeyword($query)
    {
        $keyword = request()->keyword;
        if ($keyword) {
            $keyword = trim($keyword);
            return $query->whereHas('order', function ($sql) use($keyword) {
                $sql->where('name', 'like', '%' . $keyword . '%')
                    ->orWhere('mobile', 'like', '%' . $keyword . '%');
            });
        }
        return $query;
    }
}
