<?php

namespace SoftminalB2B\Support;

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

use SoftminalB2B\Models\Group;
use SoftminalB2B\Services\Settings;

class ReviewPrompt
{
    /** Per-user dismissal flag. */
    private const META = 'smb2b_review_dismissed';

    private const REVIEW_URL = 'https://wordpress.org/support/plugin/softminal-b2b-wholesale-for-woocommerce/reviews/#new-post';

    /**
     * The shape the SPA consumes from `/status`.
     *
     * @return array<string,mixed>
     */
    public static function payload(): array
    {
        return [
            'eligible' => self::eligible(),
            'url'      => self::REVIEW_URL,
        ];
    }

    /**
     * Show the prompt only to a merchant who has finished setup, configured at
     * least one group, and not already dismissed it.
     */
    private static function eligible(): bool
    {
        if (get_user_meta(get_current_user_id(), self::META, true)) {
            return false;
        }

        if (Settings::get('setup_complete') !== 'yes') {
            return false;
        }

        return count(Group::all()) > 0;
    }

    /**
     * Persist the per-user dismissal (whether they reviewed or chose "later").
     */
    public static function dismiss(): void
    {
        update_user_meta(get_current_user_id(), self::META, '1');
    }
}
