<?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\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

#[ScopedBy([YearScope::class])]
class MafOrder extends Model
{
    use SoftDeletes;
    protected static function boot(): void
    {
        parent::boot();
        static::creating(function($attributes) {
            if(!isset($attributes->year)) {
                $attributes->year = year();
            }
        });
    }

    protected $fillable = [
        'year',
        'order_number',
        'status',
        'user_id',
        'product_id',
        'quantity',
        'in_stock',
    ];

    const DEFAULT_SORT_BY = 'created_at';

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    public function product(): BelongsTo
    {
        return $this->belongsTo(Product::class);
    }

    public function products_sku(): HasMany
    {
        return $this->hasMany(ProductSKU::class);
    }

}
