<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class ShopCatalogItem extends Model
{
    protected $fillable = [
        'branch_id', 'title', 'slug', 'kind', 'description', 'image', 'features',
        'sort_order', 'is_active',
    ];

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

    public function branch(): BelongsTo
    {
        return $this->belongsTo(ShopBranch::class, 'branch_id');
    }

    public function products(): BelongsToMany
    {
        return $this->belongsToMany(
            ShopProduct::class,
            'shop_catalog_item_product',
            'catalog_item_id',
            'product_id'
        )->withPivot('sort_order')->orderByPivot('sort_order');
    }
}
