<?php

namespace App\Models;

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

class Via extends Model
{
    use HasFactory;

    protected $table = 'via';
    protected $primaryKey = 'id_via';
    public $timestamps = true;

    protected $fillable = [
        'nombre_via',
        'numero_calle',
        'referencia',
        'id_sector',
        'longitud',
        'estado',
    ];

    // Relaciones
    public function sector()
    {
        return $this->belongsTo(Sector::class, 'id_sector');
    }

    public function conexiones()
    {
        return $this->hasMany(Conexion::class, 'id_via');
    }

    // ✅ Scope corregido
    public function scopeDelSector($query, $sectorId)
    {
        return $query->whereHas('sector', function ($s) use ($sectorId) {
            $s->where('id_sector', $sectorId);
        });
    }
}
