<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Model;

class Pedido extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',  
    ];

    /**
     * El barrio al que pertenece el pedido.
     */
    public function barrio(): BelongsTo
    {
        return $this->belongsTo(Barrio::class);
    }

    /**
     * Los productos que pertenecen al pedido.
     */
    public function productos(): BelongsToMany
    {
        return $this->belongsToMany(Producto::class)->withPivot(['cantidad']);
    }

    public function productosSinBonos() {
        return $this->productos()->where('bono',0)->all();
    }

    function bonos() {
        return $this->productos()->where('bono',1)->all();
    }

    function bonosDeTransporte() : int {
        $total = 0;
        foreach ($this->productosSinBonos() as $key => $producto) {
            $total += $producto->price * $producto->pivot->cantidad;
        }
        return 1 + ($total / 500);
    }
}