<?php

namespace App\Repositories\Shop;

use App\Repositories\Core\User\ShopCart;

class Shop
{
    public static function getSaleChannelAvailabilitySummary(array $saleChannelIds): array
    {
        if (empty($saleChannelIds) || ShopCart::count() === 0) {
            return [];
        }

        $cartItems = ShopCart::getContent();
        $summary = [];

        foreach ($saleChannelIds as $saleChannelId) {
            $saleChannelId = (int) $saleChannelId;
            $issues = [];
            $issueCount = 0;

            foreach ($cartItems as $item) {
                $offerId = (int) $item->id;

                if (! Offers::getPrice($offerId, 1, $saleChannelId)) {
                    $offer = Offers::get($offerId, ['article']);
                    $issues[] = $offer->article->name ?? $item->name;
                    $issueCount++;

                    if (count($issues) >= 3) {
                        continue;
                    }
                }
            }

            if (! empty($issues)) {
                $summary[$saleChannelId] = [
                    'full_count' => $issueCount,
                    'names' => array_slice($issues, 0, 3),
                ];
            }
        }

        return $summary;
    }
}
