<?php
/**
 * Shared Account Card.
 *
 * Unified `.tts-account-card` two-column layout for every connected plan:
 *   left  → plan badge, name, credits/mo (+ price for paid), connected email, action row
 *   right → credits meter, breakdown line, next reset date
 *
 * Free plans get two extra buttons in the action row (Upgrade + Top up) since
 * they don't get monthly top-ups automatically. Paid/Agency keep Refresh / My
 * Account / Disconnect.
 *
 * Used on:
 *  - Account & Billing page (Disconnect shown)
 *  - Overview/Settings page (Disconnect hidden)
 *
 * Caller must define:
 * @var array $billing_data Result of /api/v1/billing/account.
 *
 * Caller may define:
 * @var bool $tts_account_card_show_disconnect Default true. Set false to hide the
 *           Disconnect button on contexts where the action shouldn't be one click away.
 */

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

if ( empty( $billing_data ) || ! is_array( $billing_data ) ) {
    return;
}

$show_disconnect = isset( $tts_account_card_show_disconnect ) ? (bool) $tts_account_card_show_disconnect : true;

$acct_user    = $billing_data['user'] ?? array();
$acct_plan    = $billing_data['plan'] ?? array();
$acct_credits = $billing_data['credits'] ?? array();
$is_free      = 'free' === ( $acct_plan['slug'] ?? 'free' );

$credit_balance = (int) ( $acct_credits['balance'] ?? 0 );
?>

<?php
$monthly_credits = (int) ( $acct_plan['monthly_credits'] ?? ( $is_free ? 10000 : 0 ) );
$credit_total    = $monthly_credits > 0 ? $monthly_credits : max( 1, $credit_balance );
$credit_pct      = $credit_total > 0 ? min( 100, round( ( $credit_balance / $credit_total ) * 100 ) ) : 0;
$bar_color       = $credit_pct > 50 ? 'green' : ( $credit_pct > 10 ? 'amber' : 'red' );

$next_reset_iso  = $acct_credits['next_reset_at'] ?? null;
$next_reset_unix = $next_reset_iso ? strtotime( $next_reset_iso ) : 0;

$plan_label = $acct_plan['tier_name'] ?? ( $acct_plan['name'] ?? ( $is_free ? __( 'Free Plan', 'text-to-speech-tts' ) : '' ) );
?>

<div class="tts-account-card<?php echo $is_free ? ' tts-account-card--free' : ''; ?>">
    <div class="tts-account-status">
        <div class="tts-account-status-info">
            <div class="tts-account-plan-row">
                <span class="tts-pill <?php echo $is_free ? 'tts-pill-neutral' : 'tts-pill-success'; ?>">
                    <?php echo $is_free ? esc_html__( 'Free', 'text-to-speech-tts' ) : esc_html__( 'Active', 'text-to-speech-tts' ); ?>
                </span>
                <span class="tts-account-plan-line">
                    <?php echo esc_html( $plan_label ); ?>
                </span>
            </div>
            <p class="tts-account-plan-detail">
                <?php echo esc_html( number_format( $monthly_credits ) . ' credits/mo' ); ?>
                <?php if ( ! empty( $acct_plan['billing_interval'] ) ) : ?>
                    &middot;
                    <?php
                    if ( 'yearly' === $acct_plan['billing_interval'] ) {
                        /* translators: %s: Yearly plan price in USD. */
                        printf( esc_html__( '$%s/yr', 'text-to-speech-tts' ), esc_html( ttswp_format_price( $acct_plan['price_yearly'] ) ) );
                    } else {
                        /* translators: %s: Monthly plan price in USD. */
                        printf( esc_html__( '$%s/mo', 'text-to-speech-tts' ), esc_html( ttswp_format_price( $acct_plan['price_monthly'] ) ) );
                    }
                    ?>
                <?php endif; ?>
            </p>
            <?php if ( ! empty( $acct_user['email'] ) ) : ?>
                <p class="tts-account-email">
                    <?php
                    printf(
                        /* translators: %s: Email address wrapped in <strong> tags that identifies the connected TTSWP account. */
                        esc_html__( 'Connected as %s', 'text-to-speech-tts' ),
                        '<strong>' . esc_html( $acct_user['email'] ) . '</strong>'
                    );
                    ?>
                </p>
            <?php endif; ?>
        </div>
        <div class="tts-account-status-actions-row">
            <?php if ( $is_free ) : ?>
                <a href="<?php echo esc_url( admin_url( 'admin.php?page=text-to-speech-tts-upgrade' ) ); ?>" class="tts-btn tts-btn-primary">
                    <?php esc_html_e( 'Upgrade Plan', 'text-to-speech-tts' ); ?>
                </a>
                <a href="<?php echo esc_url( admin_url( 'admin.php?page=text-to-speech-tts-account#top-ups' ) ); ?>" class="tts-btn">
                    + <?php esc_html_e( 'Top up', 'text-to-speech-tts' ); ?>
                </a>
            <?php endif; ?>
            <a href="<?php echo esc_url( admin_url( 'admin.php?page=text-to-speech-tts-account&refresh=1' ) ); ?>" class="tts-btn">
                <?php esc_html_e( 'Refresh', 'text-to-speech-tts' ); ?>
            </a>
            <a href="<?php echo esc_url( Mementor_TTS_Connect::get_app_url() . '/dashboard' ); ?>" target="_blank" class="tts-btn">
                <?php esc_html_e( 'My Account', 'text-to-speech-tts' ); ?>
            </a>
            <?php if ( $show_disconnect ) : ?>
                <a href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?page=text-to-speech-tts-settings&tts_action=disconnect' ), 'tts_disconnect' ) ); ?>" class="tts-btn tts-btn-disconnect" onclick="return confirm('<?php esc_attr_e( 'Are you sure you want to disconnect this site?', 'text-to-speech-tts' ); ?>');">
                    <?php esc_html_e( 'Disconnect', 'text-to-speech-tts' ); ?>
                </a>
            <?php endif; ?>
        </div>
    </div>

    <div class="tts-credits-meter">
        <h3><?php esc_html_e( 'Credits Remaining', 'text-to-speech-tts' ); ?></h3>
        <div class="tts-credits-numbers">
            <span class="tts-credits-balance"><?php echo esc_html( number_format( $credit_balance ) ); ?></span>
            <?php if ( $monthly_credits > 0 ) : ?>
                <span class="tts-credits-total">/ <?php echo esc_html( number_format( $monthly_credits ) ); ?></span>
            <?php endif; ?>
        </div>
        <div class="tts-credits-bar">
            <div class="tts-credits-bar-fill <?php echo esc_attr( $bar_color ); ?>"
                 style="width: <?php echo esc_attr( $credit_pct ); ?>%"></div>
        </div>
        <?php if ( 'agency' === ( $acct_plan['source'] ?? '' ) ) : ?>
            <p class="tts-credits-breakdown">
                <?php
                printf(
                    /* translators: 1: Credits allocated to this site from the Agency pool. 2: Credits already used. */
                    esc_html__( 'Allocated: %1$s · Used: %2$s', 'text-to-speech-tts' ),
                    number_format( $acct_credits['allocated'] ?? 0 ),
                    number_format( $acct_credits['used'] ?? 0 )
                );
                ?>
            </p>
        <?php else : ?>
            <p class="tts-credits-breakdown">
                <?php
                printf(
                    /* translators: 1: Monthly credit balance that resets each billing cycle. 2: Purchased top-up credits that never expire. */
                    esc_html__( 'Monthly: %1$s · Topped up: %2$s', 'text-to-speech-tts' ),
                    number_format( $acct_credits['monthly_balance'] ?? 0 ),
                    number_format( $acct_credits['purchased_balance'] ?? 0 )
                );
                ?>
            </p>
        <?php endif; ?>
        <?php if ( $next_reset_unix > 0 ) : ?>
            <p class="tts-credits-reset-line">
                <?php
                printf(
                    /* translators: %s: Date when monthly credits reset (formatted using site's date_format option). */
                    esc_html__( 'Next reset: %s', 'text-to-speech-tts' ),
                    esc_html( wp_date( get_option( 'date_format' ), $next_reset_unix ) )
                );
                ?>
            </p>
        <?php endif; ?>
    </div>
</div>
