<?php

namespace App\Modules\Bookings;

use App\Models\ValidationModel;
use App\Models\User;
use App\Modules\Stock\Stock;

class Part extends ValidationModel
{
    use \Venturecraft\Revisionable\RevisionableTrait;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['related_id', 'related_type', 'stock_id', 'quantity', 'price', 'vat', 'quantity_on_order'];

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'parts';

    /**
     * Array of validation rules.
     *
     * @var  array
     */
    protected static $rules = [
        'related_id'   => 'required',
        'related_type' => 'required',
        'stock_id'     => 'required',
        'quantity'     => 'required',
        'price'        => 'required',
        'vat'          => 'required',
    ];

    /**
     * The model's attributes.
     *
     * @var array
     */
    protected $attributes = [
            'quantity_on_order'    => 0,
        ];

    /**
     * The relations to eager load on every query.
     *
     * @var array
     */
    protected $with = ['stock', 'composite_items'];

    /**
     * The "booting" method of the model.
     *
     * @return void
     */
    public static function boot()
    {
        parent::boot();

        // When a transaction is created we want to
        // update the created_by field
        static::creating(function ($model) {
            $model->created_by = getAuthUserId();
        });
    }

    /**
     * Round the number when storing with 2 decimal points.
     *
     * @param $value - the new value being passed in
     */
    public function setPriceAttribute($value)
    {
        return $this->attributes['price'] = number_format($value, 2, '.', '');
    }

    /**
     * Round the number to 2 decimals after point, when retrieving it.
     *
     * @return float
     */
    public function getPriceAttribute()
    {
        return number_format($this->attributes['price'], 2, '.', '');
    }

    /**
     * Returns the user who added the part.
     *
     * @return \User
     */
    public function creator()
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    /**
     * Returns the user who added the part.
     *
     * @return App\Modules\Bookings\Task
     */
    public function related()
    {
        return $this->morphTo();
    }

    /**
     * Returns the stock item for the part.
     *
     * @return App\Modules\Stock\StockItem
     */
    public function stock()
    {
        return $this->belongsTo(Stock::class, 'stock_id')->withTrashed();
    }

    /**
     * Relationship: query builder to return all the composite items for this part.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function composite_items()
    {
        return $this->belongsToMany(Stock::class, 'part_composite_items', 'part_id', 'stock_id')->withTimestamps()->withPivot('quantity');
    }

    /**
     * Calculates the total amount paid by the customer.
     */
    public function getAmountPaidAttribute()
    {
        $total = $this->price * $this->quantity * ($this->vat / 100 + 1);

        return number_format($total, 2, '.', '');
    }
}
