<?php

namespace App\Models;

use App\Traits\Blameable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Benefit extends Model
{
    use HasFactory, SoftDeletes, Blameable;

    protected $fillable = [
        'service_code',
        'code',
        'description',
        'active'
    ];

    protected $hidden = [
        "created_at",
        "updated_at",
        "deleted_at",
        "created_by",
        "updated_by",
        "deleted_by",
    ];

    public function scopeFilter($query, array $filters)
    {
        $query->when($filters['search'] ?? false, function ($query, $search) {
            return $query
                    ->where('service_code', 'like', "%" . $search . "%")
                    ->orWhere('code', 'like', "%" . $search . "%")
                    ->orWhereHas('plan', function ($query) use ($search) {
                        $query->where('code', 'like', "%" . $search . "%");
                    });
        });
    }

    // public function benefit()
    // {
    //     return $this->belongsTo(Benefit::class, 'benefit_code', 'code');
    // }

    public function claimItem()
    {
        return $this->morphMany(ClaimItem::class, 'claim_itemable');
    }

    public function plans()
    {
        return $this->belongsToMany(Plan::class, 'corporate_benefits', 'benefit_id', 'id')
                    ->withTimestamps()
                    ->withPivot([
                        // TODO corporate_benefits pivot
                    ]);
    }
}
