<?php
namespace App\Models;

use Core\Model;

class Product extends Model
{
    protected $table = 'products';
    protected $orderBy = 'sort_order';

    public function byCategory(int $catId): array
    {
        return $this->whereAll('category_id', $catId);
    }
    public function featured(int $limit = 6): array
    {
        return array_slice(array_filter($this->all(), fn($p) => ($p['status'] ?? 1) == 1), 0, $limit);
    }
    public function specsArray($p): array
    {
        $s = $p['specs'] ?? '';
        if (is_array($s)) return $s;
        $dec = json_decode((string)$s, true);
        return is_array($dec) ? $dec : [];
    }
    public function galleryArray($p): array
    {
        $g = $p['gallery'] ?? '';
        if (is_array($g)) return $g;
        $dec = json_decode((string)$g, true);
        return is_array($dec) ? $g : [];
    }
}
