<?php

namespace App\Models;

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

class Law extends Model
{
    use HasFactory;

    protected $fillable = ['title', 'is_locked', 'category_id', 'price','image'];

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


    public function category()
    {
        return $this->belongsTo(Category::class, 'category_id');
    }

    public function sections()
    {
        return $this->hasMany(Section::class);
    }

    public function gates()
    {
        return $this->hasMany(Gate::class);
    }

    public function parts()
    {
        return $this->hasMany(Part::class);
    }

    public function chapters()
    {
        return $this->hasMany(Chapter::class);
    }

    public function divisions()
    {
        return $this->hasMany(Division::class);
    }

    public function branchs()
    {
        return $this->hasMany(Branch::class);
    }

    public function arts()
    {
        return $this->hasMany(Art::class);
    }

    public function volums()
    {
        return $this->hasMany(Volum::class);
    }

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

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

    public function getIsLockedAttribute($value)
    {
        return (bool) $value;
    }

    public function setIsLockedAttribute($value)
    {
        $this->attributes['is_locked'] = $value ? 1 : 0;
    }

    public function getImageAttribute($value)
    {
        return $value ? secure_asset('images/' . $value) : null;
    }
}
