<?php

namespace App\Models;

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

class Art extends Model
{
    use HasFactory;

    protected $fillable = ['title', 'text', 'is_free', 'number', 'chapter_id', 'part_id', 'volum_id', 'law_id', 'book_id', 'section_id', 'gate_id', 'division_id', 'branch_id'];

    protected $hidden = ['created_at', 'updated_at'];

    public function chapter()
    {
        return $this->belongsTo(Chapter::class);
    }

    public function part()
    {
        return $this->belongsTo(Part::class);
    }

    public function volum()
    {
        return $this->belongsTo(Volum::class);
    }

    public function law()
    {
        return $this->belongsTo(Law::class);
    }

    public function book()
    {
        return $this->belongsTo(Book::class);
    }

    public function section()
    {
        return $this->belongsTo(Section::class);
    }

    public function gate()
    {
        return $this->belongsTo(Gate::class);
    }

    public function judicialPrecedents()
    {
        return $this->belongsToMany(JudicialPrecedent::class, 'art_judicial_precedent');
    }

    public function legalOpinions()
    {
        return $this->belongsToMany(LegalOpinion::class, 'art_legal_opinion');
    }

    public static function search($searchTerm)
    {
        return self::where('title', 'LIKE', "%{$searchTerm}%")
            ->orWhere('text', 'like', "%{$searchTerm}%")->get();
    }

    public static function searchLimit($searchTerm)
    {
        return self::where('title', 'LIKE', "%{$searchTerm}%")
            ->orWhere('text', 'like', "%{$searchTerm}%")->limit(3)->get();
    }
}
