<?php

namespace App\Models\Catalog;

use App\Models\Concerns\Auditable;
use App\Models\Concerns\BelongsToTenant;
use App\Models\Lookups\UnitOfMeasure;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class SubCategory extends Model
{
    use Auditable, BelongsToTenant, SoftDeletes;

    protected $fillable = ['main_category_id', 'name', 'sort_code', 'description', 'is_active', 'default_base_unit_id'];

    protected function casts(): array
    {
        return ['is_active' => 'boolean'];
    }

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

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

    /**
     * Form-prefill convenience only (plan decision A6) — never read by
     * stock/pricing/conversion logic.
     */
    public function defaultBaseUnit(): BelongsTo
    {
        return $this->belongsTo(UnitOfMeasure::class, 'default_base_unit_id');
    }
}
