<?php

namespace App\Models\Sales;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * 영업 상품 카테고리 모델
 *
 * @property int $id
 * @property string $code
 * @property string $name
 * @property string|null $description
 * @property string $base_storage
 * @property int $display_order
 * @property bool $is_active
 */
class SalesProductCategory extends Model
{
    use SoftDeletes;

    protected $table = 'sales_product_categories';

    protected $fillable = [
        'code',
        'name',
        'description',
        'base_storage',
        'display_order',
        'is_active',
    ];

    protected $casts = [
        'display_order' => 'integer',
        'is_active' => 'boolean',
    ];

    /**
     * 상품 관계
     */
    public function products(): HasMany
    {
        return $this->hasMany(SalesProduct::class, 'category_id');
    }

    /**
     * 활성 상품만
     */
    public function activeProducts(): HasMany
    {
        return $this->products()->where('is_active', true)->orderBy('display_order');
    }

    /**
     * 활성 카테고리 스코프
     */
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    /**
     * 정렬 스코프
     */
    public function scopeOrdered($query)
    {
        return $query->orderBy('display_order')->orderBy('name');
    }
}
