<?php

namespace GriffinForms\Admin\App\Html\Modals\Integrations\Renders;

/**
 * GridView Renderer
 *
 * Renders a grid of all available integrations.
 * Each integration is displayed as a card with icon, name, and status badge.
 * Clicking a card shows the integration info view.
 *
 * @since 1.0.0
 */
class Gridview extends Format
{
    /**
     * Render integrations grid
     *
     * @param array $providers All available providers from Providers::getAll()
     * @return string HTML grid
     */
    public function render($data = []): string
    {
        $providers = $data['providers'] ?? [];

        if (empty($providers)) {
            return $this->renderEmpty();
        }

        // Render single continuous grid (categories handled via filter pills)
        return $this->renderGrid($providers);
    }

    /**
     * Render grid without categories
     *
     * @param array $providers All providers
     * @return string HTML
     */
    private function renderGrid(array $providers): string
    {
        $html = '<div class="gf-integrations-grid">' . PHP_EOL;

        foreach ($providers as $provider) {
            $html .= $this->renderCard($provider);
        }

        $html .= '</div>' . PHP_EOL;

        return $html;
    }

    /**
     * Render a single integration card
     *
     * @param array $provider Provider metadata
     * @return string Card HTML
     */
    private function renderCard(array $provider): string
    {
        $id = $this->escAttr($provider['id']);
        $raw_name = $provider['name'] ?? '';
        $name = $this->escHtml($raw_name);
        $is_single_word = strpos(trim($raw_name), ' ') === false;
        $name_classes = $this->getClasses([
            'gf-integration-card-name',
            $is_single_word ? 'gf-integration-card-name-single' : '',
        ]);
        $coming_soon      = !empty($provider['coming_soon']);
        $is_paid          = !empty($provider['is_paid']);
        $package_admitted = !empty($provider['package_admitted']);
        $show_pro_pill    = $is_paid && !$package_admitted;

        $card_classes = $this->getClasses([
            'gf-integration-card',
            $coming_soon    ? 'gf-integration-card-coming-soon' : '',
            $show_pro_pill  ? 'gf-integration-card-pro-locked'  : '',
        ]);

        $attr_string = $this->dataAttr('integration-id', $id);
        if ($coming_soon) {
            $attr_string .= ' ' . $this->dataAttr('coming-soon', '1');
        }
        if ($show_pro_pill) {
            $attr_string .= ' ' . $this->dataAttr('pro-locked', '1');
        }

        $html = sprintf(
            '<div class="%s" %s>' . PHP_EOL,
            $this->escAttr($card_classes),
            $attr_string
        );

        if ($show_pro_pill) {
            $html .= sprintf(
                '<span class="gf-integration-card-pill gf-integration-card-pill-pro">%s</span>' . PHP_EOL,
                $this->escHtml(__('Pro', 'griffinforms-form-builder'))
            );
        }

        if ($coming_soon) {
            $html .= sprintf(
                '<span class="gf-integration-card-pill">%s</span>' . PHP_EOL,
                $this->escHtml(__('Coming Soon', 'griffinforms-form-builder'))
            );
        }

        // Icon
        $html .= '<div class="gf-integration-card-icon">' . PHP_EOL;
        $html .= $this->renderIcon($provider) . PHP_EOL;
        $html .= '</div>' . PHP_EOL;

        // Name
        $html .= sprintf(
            '<p class="%s">%s</p>' . PHP_EOL,
            $this->escAttr($name_classes),
            $name
        );

        $html .= '</div>' . PHP_EOL;

        return $html;
    }

    /**
     * Render empty state message
     *
     * @return string HTML
     */
    private function renderEmpty(): string
    {
        return sprintf(
            '<div class="gf-integrations-empty">%s</div>',
            $this->escHtml(__('No integrations available', 'griffinforms-form-builder'))
        );
    }
}
