<?php

namespace PortalPeople\invoicing\helpers;

use PortalPeople\invoicing\models\Invoicesbe;
use backend\models\Cartsbe;
use Yii;
use yii\helpers\Html;

/**
 * Cart Invoice Helper
 *
 * Provides view helpers for displaying invoice status in cart views.
 */
class CartInvoiceHelper
{
    /**
     * Check if cart is invoiced
     *
     * @param Cartsbe $cart
     * @return bool
     */
    public static function isInvoiced(Cartsbe $cart): bool
    {
        return !empty($cart->invoiceid);
    }

    /**
     * Render invoice status badge
     *
     * @param Cartsbe $cart
     * @param array $options HTML options
     * @return string
     */
    public static function renderInvoiceStatusBadge(Cartsbe $cart, array $options = []): string
    {
        if (!self::isInvoiced($cart)) {
            return '';
        }

        $invoice = Invoicesbe::findOne($cart->invoiceid);
        if (!$invoice) {
            return '';
        }

        $badge = Html::tag('span', 'Invoiced', array_merge([
            'class' => 'badge badge-success',
            'title' => 'Invoice #' . Html::encode($invoice->invoice_number),
        ], $options));

        return $badge;
    }

    /**
     * Render invoice info panel
     *
     * @param Cartsbe $cart
     * @return string
     */
    public static function renderInvoiceInfoPanel(Cartsbe $cart): string
    {
        if (!self::isInvoiced($cart)) {
            return '';
        }

        $invoice = Invoicesbe::findOne($cart->invoiceid);
        if (!$invoice) {
            return '';
        }

        $invoiceLink = Html::a(
            'Invoice #' . Html::encode($invoice->invoice_number),
            ['//surf-invoicing/invoice/view', 'id' => $invoice->id],
            ['class' => 'btn btn-sm btn-info']
        );

        $generatedDate = $invoice->generated_at 
            ? Yii::$app->formatter->asDate($invoice->generated_at, 'php:Y-m-d')
            : 'Unknown';

        $html = Html::tag('div', implode('', [
            Html::tag('div', 'This order has been invoiced and cannot be edited directly.', ['class' => 'alert-text']),
            Html::tag('div', [
                Html::tag('strong', 'Invoice Number: '),
                Html::encode($invoice->invoice_number),
                Html::tag('br'),
                Html::tag('strong', 'Generated: '),
                $generatedDate,
                Html::tag('br'),
                Html::tag('strong', 'Amount: '),
                Yii::$app->formatter->asCurrency($invoice->total),
            ], ['style' => 'margin-top: 10px;']),
            Html::tag('div', [
                $invoiceLink,
                Html::a('Credit Note', ['//surf-invoicing/invoice/create-credit-note', 'invoiceId' => $invoice->id], ['class' => 'btn btn-sm btn-warning']),
            ], ['style' => 'margin-top: 10px;']),
        ]), [
            'class' => 'alert alert-info',
            'style' => 'margin-top: 15px;',
        ]);

        return $html;
    }

    /**
     * Check if cart can be edited
     *
     * @param Cartsbe $cart
     * @return bool
     */
    public static function canEdit(Cartsbe $cart): bool
    {
        return !self::isInvoiced($cart);
    }

    /**
     * Get invoice balance with credit note deductions
     *
     * @param Cartsbe $cart
     * @return float|null
     */
    public static function getInvoiceBalance(Cartsbe $cart): ?float
    {
        if (!self::isInvoiced($cart)) {
            return null;
        }

        $invoice = Invoicesbe::findOne($cart->invoiceid);
        return $invoice ? $invoice->getInvoiceBalance() : null;
    }

    /**
     * Get outstanding credit amount
     *
     * @param Cartsbe $cart
     * @return float|null
     */
    public static function getTotalCredit(Cartsbe $cart): ?float
    {
        if (!self::isInvoiced($cart)) {
            return null;
        }

        $invoice = Invoicesbe::findOne($cart->invoiceid);
        return $invoice ? $invoice->getTotalCreditAmount() : null;
    }
}
