<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class CostCenterKeyword extends Model
{
    use HasFactory;

    protected $fillable = [
        'cost_center_id',
        'keyword',
        'is_case_sensitive',
        'is_active',
    ];

    protected $casts = [
        'is_case_sensitive' => 'boolean',
        'is_active' => 'boolean',
    ];

    /**
     * Relação com o centro de custo
     */
    public function costCenter(): BelongsTo
    {
        return $this->belongsTo(CostCenter::class);
    }

    /**
     * Scope para keywords ativas
     */
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    /**
     * Verifica se o texto contém esta palavra-chave
     */
    public function matchesText(string $text): bool
    {
        if ($this->is_case_sensitive) {
            return str_contains($text, $this->keyword);
        }
        return str_contains(strtolower($text), strtolower($this->keyword));
    }
}
