<?php
namespace Components\Models;

class ProductToCategory extends BaseModel
{
    protected static string $table = 'products_to_categories';
    protected static string $primaryKey = 'products_id';

    /**
     * @return int[]
     */
    public static function categoryIdsForProduct(int $productId): array
    {
        if (self::isDev() || $productId <= 0) {
            return [];
        }

        $q = \mh_db_query(
            "SELECT categories_id FROM " . static::table() .
            " WHERE products_id = " . (int)$productId .
            " ORDER BY categories_id"
        );

        $ids = [];
        while ($row = \mh_db_fetch_array($q)) {
            $id = (int)($row['categories_id'] ?? 0);
            if ($id > 0) {
                $ids[$id] = $id;
            }
        }
        \mh_db_free_result($q);

        ksort($ids);
        return array_values($ids);
    }

    /**
     * @return int[]
     */
    public static function productIdsForCategory(int $categoryId): array
    {
        if (self::isDev() || $categoryId <= 0) {
            return [];
        }

        $q = \mh_db_query(
            "SELECT products_id FROM " . static::table() .
            " WHERE categories_id = " . (int)$categoryId .
            " ORDER BY products_id"
        );

        $ids = [];
        while ($row = \mh_db_fetch_array($q)) {
            $id = (int)($row['products_id'] ?? 0);
            if ($id > 0) {
                $ids[$id] = $id;
            }
        }
        \mh_db_free_result($q);

        ksort($ids);
        return array_values($ids);
    }

    /**
     * @param mixed $productIds
     */
    public static function syncForCategory(int $categoryId, $productIds): void
    {
        if (self::isDev() || $categoryId <= 0) {
            return;
        }

        $ids = static::normalizeIdList($productIds);
        \mh_db_query("DELETE FROM " . static::table() . " WHERE categories_id = " . (int)$categoryId);

        foreach ($ids as $productId) {
            \mh_db_perform(static::table(), [
                'products_id' => $productId,
                'categories_id' => (int)$categoryId,
            ], 'insert');
        }
    }

    /**
     * @param mixed $categoryIds
     */
    public static function syncForProduct(int $productId, $categoryIds): void
    {
        if (self::isDev() || $productId <= 0) {
            return;
        }

        $ids = static::normalizeIdList($categoryIds);
        \mh_db_query("DELETE FROM " . static::table() . " WHERE products_id = " . (int)$productId);

        foreach ($ids as $categoryId) {
            \mh_db_perform(static::table(), [
                'products_id' => (int)$productId,
                'categories_id' => $categoryId,
            ], 'insert');
        }
    }

    /**
     * @param mixed $values
     * @return int[]
     */
    protected static function normalizeIdList($values): array
    {
        if (!is_array($values)) {
            $values = [$values];
        }

        $ids = [];
        foreach ($values as $value) {
            $id = (int)$value;
            if ($id > 0) {
                $ids[$id] = $id;
            }
        }

        ksort($ids);
        return array_values($ids);
    }
}
