<?php

namespace App\Models;

use APP\Models\equipament_type;
use App\Models\factorie;
use App\Models\specific_Attributes_Equipament_Type;
use App\Models\installation;

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

class Equipment extends Model
{
    use HasFactory;

    public $timestamps = false;

    protected $table = 'equipments';

    protected $primaryKey = 'equipment_id';

    public function unit()
    {
        return $this->belongsTo(Unit::class, 'unit_id', 'unit_id');
    }

    public function equipmentType()
    {
        return $this->belongsTo(EquipmentType::class, 'equipment_type_id', 'equipment_type_id');
    }

    public function specificAttributes()
    {
        return $this->hasMany(SpecificAttributesEquipmentType::class, 'equipment_id', 'equipment_id')
            ->join('general_attributes_equipaments', 'specific_attributes_equipament_types.general_attributes_equipment_id', '=', 'general_attributes_equipaments.general_attributes_equipment_id')
            ->orderBy('general_attributes_equipaments.general_attributes_equipment_description', 'asc');
    }

    public function equipmentAssociationAmbit()
    {
        //Antiga hasMany , apenas colocado hasOne para funcionar o datatables
        return $this->hasOne(EquipmentAssociationAmbit::class, 'equipment_id', 'equipment_id');
    }

    public function orderEquipmentTasks()
    {
        return $this->hasMany(OrderEquipmentTasks::class, 'equipment_id', 'equipment_id');
    }

    public function equipmentComments()
    {
        return $this->hasMany(EquipmentComments::class, 'equipment_id', 'equipment_id');
    }
    
    public function controlEquipmentWorkstation()
    {
        return $this->hasMany(ControlEquipmentWorkstation::class, 'equipment_id', 'equipment_id');
    }

    public function hasInspectionYes()
    {
        return $this->orderEquipmentTasks()->where('inspection', 'Sim')->exists();
    }

 
}
