<?php
/**
 * Shortcode Manager for Easy Invoice Free
 * 
 * Manages shortcodes for invoice and quote URLs
 *
 * @package EasyInvoice
 * @subpackage Shortcodes
 * @since 2.0.0
 */

namespace EasyInvoice\Shortcodes;

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

/**
 * ShortcodeManager Class
 */
class ShortcodeManager {
    
    /**
     * Constructor
     */
    public function __construct() {
        // Register shortcodes
        add_shortcode('easy_invoice_url', [$this, 'renderInvoiceUrl']);
        add_shortcode('easy_quote_url', [$this, 'renderQuoteUrl']);
        
        // Add shortcode info to help tab
        add_action('admin_head', [$this, 'addShortcodeHelp']);
    }

    /**
     * Render invoice URL shortcode
     *
     * @param array $atts Shortcode attributes
     * @return string Rendered shortcode
     */
    public function renderInvoiceUrl($atts) {
        $atts = shortcode_atts([
            'id' => 0,
            'number' => '',
            'text' => '',
            'class' => 'easy-invoice-url',
            'target' => '_blank'
        ], $atts, 'easy_invoice_url');

        // Get invoice by ID or number
        $invoice = null;
        if (!empty($atts['id'])) {
            $invoice = \EasyInvoice\Providers\InvoiceServiceProvider::getInvoiceRepository()->find($atts['id']);
        } elseif (!empty($atts['number'])) {
            $invoice = \EasyInvoice\Providers\InvoiceServiceProvider::getInvoiceRepository()->findByNumber($atts['number']);
        }

        if (!$invoice) {
            return '<span class="easy-invoice-error">' . __('Invoice not found', 'easy-invoice') . '</span>';
        }

        $url = get_permalink($invoice->getId());
        // Only use secure link if enabled in settings and available
        $secure_links_enabled = get_option('easy_invoice_pro_enable_secure_links', 'no') === 'yes';
        if ($secure_links_enabled && class_exists('\EasyInvoicePro\Controllers\PermalinkController')) {
            $secure_url = \EasyInvoicePro\Controllers\PermalinkController::getInvoiceSecureLinkUrl($invoice->getId());
            if ($secure_url) {
                $url = $secure_url;
            }
        }
        $text = !empty($atts['text']) ? $atts['text'] : $invoice->getNumber();
        $class = esc_attr($atts['class']);
        $target = esc_attr($atts['target']);

        return sprintf(
            '<a href="%s" class="%s" target="%s">%s</a>',
            esc_url($url),
            $class,
            $target,
            esc_html($text)
        );
    }

    /**
     * Render quote URL shortcode
     *
     * @param array $atts Shortcode attributes
     * @return string Rendered shortcode
     */
    public function renderQuoteUrl($atts) {
        $atts = shortcode_atts([
            'id' => 0,
            'number' => '',
            'text' => '',
            'class' => 'easy-quote-url',
            'target' => '_blank'
        ], $atts, 'easy_quote_url');

        // Get quote by ID or number
        $quote = null;
        if (!empty($atts['id'])) {
            $quote = \EasyInvoice\Providers\QuoteServiceProvider::getQuoteRepository()->find($atts['id']);
        } elseif (!empty($atts['number'])) {
            $quote = \EasyInvoice\Providers\QuoteServiceProvider::getQuoteRepository()->findByNumber($atts['number']);
        }

        if (!$quote) {
            return '<span class="easy-invoice-error">' . __('Quote not found', 'easy-invoice') . '</span>';
        }

        $url = get_permalink($quote->getId());
        $secure_links_enabled = get_option('easy_invoice_pro_enable_secure_links', 'no') === 'yes';
        if ($secure_links_enabled && class_exists('\EasyInvoicePro\Controllers\PermalinkController')) {
            $secure_url = \EasyInvoicePro\Controllers\PermalinkController::getQuoteSecureLinkUrl($quote->getId());
            if ($secure_url) {
                $url = $secure_url;
            }
        }
        $text = !empty($atts['text']) ? $atts['text'] : $quote->getNumber();
        $class = esc_attr($atts['class']);
        $target = esc_attr($atts['target']);

        return sprintf(
            '<a href="%s" class="%s" target="%s">%s</a>',
            esc_url($url),
            $class,
            $target,
            esc_html($text)
        );
    }

    /**
     * Add shortcode help to the help tab
     */
    public function addShortcodeHelp() {
        $screen = get_current_screen();
        
        if ($screen && ($screen->id === 'easy-invoice_page_easy-invoice-settings' || $screen->id === 'edit-easy_invoice')) {
            $screen->add_help_tab([
                'id' => 'easy-invoice-shortcodes',
                'title' => __('Shortcodes', 'easy-invoice'),
                'content' => $this->getShortcodeHelpContent()
            ]);
        }
    }

    /**
     * Get shortcode help content
     * 
     * @return string Help content
     */
    private function getShortcodeHelpContent() {
        $content = '<h2>' . __('Available Shortcodes', 'easy-invoice') . '</h2>';
        
        $content .= '<h3><code>[easy_invoice_url]</code></h3>';
        $content .= '<p>' . __('Displays a link to an invoice.', 'easy-invoice') . '</p>';
        $content .= '<h4>' . __('Attributes:', 'easy-invoice') . '</h4>';
        $content .= '<ul>';
        $content .= '<li><code>id</code> - ' . __('Invoice ID (required if number is not provided)', 'easy-invoice') . '</li>';
        $content .= '<li><code>number</code> - ' . __('Invoice number (required if id is not provided)', 'easy-invoice') . '</li>';
        $content .= '<li><code>text</code> - ' . __('Link text (default: invoice number)', 'easy-invoice') . '</li>';
        $content .= '<li><code>class</code> - ' . __('CSS class for the link (default: easy-invoice-url)', 'easy-invoice') . '</li>';
        $content .= '<li><code>target</code> - ' . __('Link target (default: _blank)', 'easy-invoice') . '</li>';
        $content .= '</ul>';
        $content .= '<h4>' . __('Examples:', 'easy-invoice') . '</h4>';
        $content .= '<pre><code>[easy_invoice_url id="123" text="View Invoice"]</code></pre>';
        $content .= '<pre><code>[easy_invoice_url number="INV-001" text="Click here to view"]</code></pre>';
        
        $content .= '<hr>';
        
        $content .= '<h3><code>[easy_quote_url]</code></h3>';
        $content .= '<p>' . __('Displays a link to a quote.', 'easy-invoice') . '</p>';
        $content .= '<h4>' . __('Attributes:', 'easy-invoice') . '</h4>';
        $content .= '<ul>';
        $content .= '<li><code>id</code> - ' . __('Quote ID (required if number is not provided)', 'easy-invoice') . '</li>';
        $content .= '<li><code>number</code> - ' . __('Quote number (required if id is not provided)', 'easy-invoice') . '</li>';
        $content .= '<li><code>text</code> - ' . __('Link text (default: quote number)', 'easy-invoice') . '</li>';
        $content .= '<li><code>class</code> - ' . __('CSS class for the link (default: easy-quote-url)', 'easy-invoice') . '</li>';
        $content .= '<li><code>target</code> - ' . __('Link target (default: _blank)', 'easy-invoice') . '</li>';
        $content .= '</ul>';
        $content .= '<h4>' . __('Examples:', 'easy-invoice') . '</h4>';
        $content .= '<pre><code>[easy_quote_url id="456" text="View Quote"]</code></pre>';
        $content .= '<pre><code>[easy_quote_url number="QT-001" text="Click here to view"]</code></pre>';
        
        return $content;
    }
}
