<?php

namespace App\Models;

use App\Filters\FarmFilters;
use Essa\APIToolKit\Filters\Filterable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Farm extends Model
{
    use HasFactory, Filterable, SoftDeletes;

    protected string $default_filters = FarmFilters::class;

    protected $table = "farms";

    protected $fillable = ["name", "code", "area_id", "lead_man_id", "lead_man_name"];

    public function area()
    {
        return $this->belongsTo(Area::class, "area_id", "id");
    }

    public function buildings()
    {
        return $this->hasMany(Building::class, "farm_id", "id");
    }

    public function leadman()
    {
        return $this->belongsTo(User::class, "lead_man_id", "id");
    }

    public function scopeActive(Builder $query): Builder
    {
        return $query->whereNull("deleted_at");
    }
}
