<?php

namespace EasyInvoice\Helpers;

/**
 * Helper class for managing quote-invoice relationships
 */
class QuoteInvoiceHelper
{
    /**
     * Check if an invoice was converted from a quote
     *
     * @param int $invoice_id
     * @return bool
     */
    public static function isInvoiceConvertedFromQuote($invoice_id)
    {
        // Check if this invoice was converted from a quote
        $quote_id = get_post_meta($invoice_id, '_converted_from_quote', true);
        return !empty($quote_id);
    }

    /**
     * Get quote information for an invoice
     *
     * @param int $invoice_id
     * @return array|false Quote information or false if not found
     */
    public static function getQuoteInfoFromInvoice($invoice_id)
    {
        // Check if this invoice was converted from a quote
        $quote_id = get_post_meta($invoice_id, '_converted_from_quote', true);

        if (!$quote_id) {
            return false;
        }

        // Get quote post
        $quote_post = get_post($quote_id);
        if (!$quote_post || $quote_post->post_type !== 'easy_invoice_quote') {
            return false;
        }

        // Get quote number
        $quote_number = get_post_meta($quote_id, '_quote_number', true);

        // Get quote title
        $quote_title = $quote_post->post_title;

        // Determine icon and tooltip
        $icon_class = 'fas fa-exchange-alt';
        $tooltip_text = sprintf(__('Converted from Quote %s', 'easy-invoice'), $quote_number ?: $quote_title);

        return [
            'id' => $quote_id,
            'number' => $quote_number,
            'title' => $quote_title,
            'icon_class' => $icon_class,
            'tooltip_text' => $tooltip_text,
            'url' => admin_url('admin.php?page=easy-invoice-quote-builder&id=' . $quote_id)
        ];
    }
}
