<?php

namespace App\Models;

use App\Support\ClientSearchCriteriaRemark;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class ClientSearchLog extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id',
        'criteria',
        'result_count',
        'ip_address',
        'status',
    ];

    protected $casts = [
        'criteria' => 'array',
    ];

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function getCriteriaRemarkAttribute(): string
    {
        $remark = ClientSearchCriteriaRemark::describe($this->criteria);

        $status = $this->attributes['status'] ?? $this->status ?? 'search';
        if ($status === 'print') {
            $remark .= ' · Print / PDF export';
        } elseif ($status === 'excel_download') {
            $remark .= ' · Excel export';
        }

        return $remark;
    }
}
