<?php

namespace App\Models;

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

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

    protected string $default_filters = BuildingFilters::class;

    protected $fillable = ["name", "farm_id"];

    public function farm()
    {
        return $this->belongsTo(Farm::class, "farm_id", "id");
    }

    public function batches()
    {
        return $this->hasMany(Batches::class, "building_id", "id");
    }

    public function activeBatch()
    {
        return $this->hasOne(Batches::class)
            ->where("status", "active");
    }

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