<?php
/**
 * Admin dish list filters (dashboard “needs attention” links).
 *
 * @package Maca_Menu_List
 */

if (!defined('ABSPATH')) {
    exit;
}

/**
 * Allowed issue filter keys for the dishes list.
 *
 * @return array<int, string>
 */
function maca_menulist_get_dishes_list_issue_filters() {
    return array(
        'no_image',
        'no_allergens',
        'no_title',
        'no_price',
    );
}

/**
 * Sanitize dishes list issue query arg.
 *
 * @param string $issue Raw issue key.
 * @return string Empty string if invalid.
 */
function maca_menulist_sanitize_dishes_list_issue($issue) {
    $issue = sanitize_key((string) $issue);

    return in_array($issue, maca_menulist_get_dishes_list_issue_filters(), true) ? $issue : '';
}

/**
 * Admin URL for the dishes list, optionally filtered by issue.
 *
 * @param string $issue Issue filter key.
 * @return string
 */
function maca_menulist_admin_dishes_list_url($issue = '') {
    $url = admin_url('admin.php?page=maca-restu-dishes');
    $issue = maca_menulist_sanitize_dishes_list_issue($issue);

    if ($issue !== '') {
        $url = add_query_arg('issue', $issue, $url);
    }

    return $url;
}

/**
 * Human-readable label for an issue filter.
 *
 * @param string $issue Issue key.
 * @return string
 */
function maca_menulist_get_dishes_list_issue_label($issue) {
    $issue = maca_menulist_sanitize_dishes_list_issue($issue);

    $labels = array(
        'no_image' => __('Dishes without image', 'maca-restu-pro'),
        'no_allergens' => __('Dishes missing allergen info', 'maca-restu-pro'),
        'no_title' => __('Dishes missing title', 'maca-restu-pro'),
        'no_price' => __('Dishes without price', 'maca-restu-pro'),
    );

    return isset($labels[ $issue ]) ? $labels[ $issue ] : '';
}

/**
 * Whether a dish row matches an issue filter (same rules as the dashboard counts).
 *
 * @param object $dish  Dish row from maca_menulist_db_get_dishes().
 * @param string $issue Issue key.
 * @return bool
 */
function maca_menulist_dish_matches_list_issue($dish, $issue) {
    $issue = maca_menulist_sanitize_dishes_list_issue($issue);
    if ($issue === '' || !is_object($dish)) {
        return true;
    }

    if ((int) $dish->is_active !== 1) {
        return false;
    }

    switch ($issue) {
        case 'no_image':
            return empty($dish->image_url);

        case 'no_allergens':
            if (empty(maca_menulist_db_get_allergens())) {
                return false;
            }
            return empty(maca_menulist_db_get_dish_allergen_ids((int) $dish->id));

        case 'no_title':
            if (maca_menulist_admin_show_local_content_fields()) {
                return trim((string) ($dish->title ?? '')) === ''
                    && trim((string) ($dish->title_en ?? '')) === '';
            }
            return trim((string) ($dish->title ?? '')) === '';

        case 'no_price':
            return $dish->price === null || $dish->price === '' || (float) $dish->price <= 0;
    }

    return false;
}

/**
 * Filter dish list rows by dashboard issue type.
 *
 * @param array<int, object> $dishes Dishes from maca_menulist_db_get_dishes().
 * @param string             $issue  Issue key.
 * @return array<int, object>
 */
function maca_menulist_filter_dishes_for_list_issue($dishes, $issue) {
    $issue = maca_menulist_sanitize_dishes_list_issue($issue);
    if ($issue === '') {
        return $dishes;
    }

    $filtered = array();
    foreach ($dishes as $dish) {
        if (maca_menulist_dish_matches_list_issue($dish, $issue)) {
            $filtered[] = $dish;
        }
    }

    return $filtered;
}

/**
 * Lowercase search blob for a dish row (admin list client-side filter).
 *
 * @param object $dish Dish row from maca_menulist_db_get_dishes().
 * @return string
 */
function maca_menulist_get_dish_list_search_blob($dish) {
    if (!is_object($dish)) {
        return '';
    }

    $parts = array(
        (string) ($dish->id ?? ''),
        (string) ($dish->title ?? ''),
        (string) ($dish->title_en ?? ''),
        (string) ($dish->category_name ?? ''),
        (string) ($dish->description ?? ''),
        (string) ($dish->description_en ?? ''),
        (string) ($dish->price ?? ''),
        (string) ($dish->price_takeaway ?? ''),
        (string) ($dish->vat_code ?? ''),
    );

    if (function_exists('maca_menulist_get_admin_content_label')) {
        $parts[] = maca_menulist_get_admin_content_label(
            $dish->title ?? '',
            $dish->title_en ?? ''
        );
    }

    if ($dish->price !== null && $dish->price !== '' && function_exists('maca_menulist_format_price')) {
        $parts[] = maca_menulist_format_price($dish->price);
    }

    $blob = implode(' ', array_filter(array_map('trim', $parts)));
    $blob = function_exists('remove_accents') ? remove_accents($blob) : $blob;

    return strtolower($blob);
}
