<?php
declare(strict_types=1);
namespace Services;

use Repositories\ProductRepository;

final class ProductService
{
    public function __construct(private ?ProductRepository $repo = null) { $this->repo ??= new ProductRepository(); }

    public function list(array $filters): array { return $this->repo->all($filters); }

    public function detail(string $slug): ?array
    {
        $product = $this->repo->findBySlug($slug);
        if (!$product) return null;
        $product['images'] = $this->repo->images((int)$product['id']);
        $product['related_products'] = $this->repo->related((int)$product['id'], (int)$product['category_id']);
        $product['schema'] = [
            '@context' => 'https://schema.org', '@type' => 'Product',
            'name' => $product['name'], 'description' => $product['short_description'],
            'sku' => $product['sku'], 'offers' => ['@type' => 'Offer', 'priceCurrency' => 'INR', 'price' => $product['price'], 'availability' => ((int)$product['stock_quantity'] > 0 ? 'https://schema.org/InStock' : 'https://schema.org/OutOfStock')]
        ];
        return $product;
    }
}
