<?php

namespace App\Models;

use App\Models\Scopes\YearScope;
use Illuminate\Database\Eloquent\Attributes\ScopedBy;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

#[ScopedBy([YearScope::class])]
class ProductSKU extends Model
{
    use SoftDeletes;

    const DEFAULT_SORT_BY = 'created_at';
    protected $fillable = [
        'year',
        'product_id',
        'order_id',
        'maf_order_id',
        'status',               // needs - нуждается в данном товаре, related - привязана к MafOrder
        'rfid',
        'factory_number',
        'manufacture_date',
        'service_life',
        'certificate_number',
        'certificate_date',
        'certificate_issuer',
        'certificate_type',
        'statement_number',
        'statement_date',
        'upd_number',
        'comment',
    ];
    public $table = 'products_sku';

    // set year attribute to current selected year
    protected static function boot(): void
    {
        parent::boot();
        static::creating(function($attributes) {
            if(!isset($attributes->year)) {
                $attributes->year = year();
            }
        });
    }

    /**
     * @return BelongsTo
     */
    public function product(): BelongsTo
    {
        return $this->belongsTo(Product::class, 'product_id', 'id');
    }

    /**
     * @return BelongsTo
     */
    public function order(): BelongsTo
    {
        return $this->belongsTo(Order::class, 'order_id', 'id');
    }

    /**
     * @return BelongsTo
     */
    public function maf_order(): BelongsTo
    {
        return $this->belongsTo(MafOrder::class, 'maf_order_id', 'id');
    }
}
