<?php

namespace GriffinForms\Admin\Html\Elements;

class UpsellShell extends \GriffinForms\Admin\Html\Format
{
    protected array $view_model = [];

    protected function setViewModel()
    {
        $this->view_model = is_array($this->view_model) ? $this->view_model : [];
    }

    public function __construct(array $view_model = [])
    {
        $this->view_model = $view_model;
        $this->setAll();
    }

    public function html(): void
    {
        $shape = sanitize_key((string) ($this->view_model['shape'] ?? 'card'));
        $classes = ['gf-upsell-shell', 'gf-upsell-shell-' . $shape];
        $title = (string) ($this->view_model['title'] ?? '');
        $description = (string) ($this->view_model['description'] ?? '');
        $status_message = (string) ($this->view_model['status_message'] ?? '');
        $cta_text = (string) ($this->view_model['cta_text'] ?? '');
        $cta_url = (string) ($this->view_model['cta_url'] ?? '');
        $badge = (string) ($this->view_model['badge'] ?? '');

        echo '<div class="' . esc_attr(implode(' ', $classes)) . '">';
        if ($shape !== 'overlay') {
            echo '<div class="gf-upsell-shell__icon" aria-hidden="true">' . wp_kses_post($this->getLockIcon()) . '</div>';
        }
        echo '<div class="gf-upsell-shell__body">';
        if ($badge !== '') {
            if (($this->view_model['package_id'] ?? '') === 'pro') {
                $pro_badge = new \GriffinForms\Admin\Html\Elements\ProBadge('Pro', 'gradient');
                $pro_badge->html();
            } else {
                echo '<div class="gf-upsell-shell__badge">';
                echo esc_html($badge);
                echo '</div>';
            }
        }
        if ($title !== '') {
            echo '<h3 class="gf-upsell-shell__title">' . esc_html($title) . '</h3>';
        }
        if ($description !== '') {
            echo '<p class="gf-upsell-shell__description">' . esc_html($description) . '</p>';
        }
        if ($status_message !== '') {
            echo '<div class="gf-upsell-shell__status" role="note">';
            echo '<p class="gf-upsell-shell__status-text">' . esc_html($status_message) . '</p>';
            echo '</div>';
        }
        if ($cta_text !== '' && $cta_url !== '') {
            echo '<a class="button button-primary gf-upsell-shell__cta" href="' . esc_url($cta_url) . '">' . esc_html($cta_text) . '</a>';
        }
        echo '</div>';
        echo '</div>';
    }

    protected function getLockIcon(): string
    {
        return '<svg viewBox="0 0 24 24" width="20" height="20" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M7 10V7.5C7 4.74 9.24 2.5 12 2.5C14.76 2.5 17 4.74 17 7.5V10" stroke="currentColor" stroke-width="1.75" stroke-linecap="round"/><rect x="4.5" y="10" width="15" height="11" rx="2.5" stroke="currentColor" stroke-width="1.75"/><path d="M12 14.25V16.75" stroke="currentColor" stroke-width="1.75" stroke-linecap="round"/></svg>';
    }
}
