<?php
/**
 * BYOK (ElevenLabs) status strip — shared between the two Quick Stats layouts
 * on the Overview / Settings page. Renders one of 3 states:
 *
 *   1. No key stored           → purple "Hint!" callout promoting BYOK
 *   2. Key stored but invalid  → red "ElevenLabs key invalid" pill + Update link
 *   3. Key stored and valid    → green "ElevenLabs connected" pill + tier + credits/month
 *
 * Reads these variables from the including partial:
 *   - $has_byok             (bool)   True when the site has an ElevenLabs key stored on the SaaS.
 *   - $qs_elevenlabs        (array)  Fresh EL subscription data from /v1/credits, or null.
 *   - $qs_elevenlabs_error  (array)  Error payload when EL rejected the stored key, or null.
 *   - $tts_byok_hint_variant (string) 'inline' (default) or 'stack' for the Hint! layout.
 *
 * @package TTSWP
 */

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

$tts_byok_hint_variant = ( isset( $tts_byok_hint_variant ) && 'stack' === $tts_byok_hint_variant ) ? 'stack' : 'inline';
?>

<?php if ( empty( $has_byok ) ) : ?>
<div class="tts-byok-hint tts-byok-hint--<?php echo esc_attr( $tts_byok_hint_variant ); ?>">
    <div class="tts-byok-hint-lead">
        <span class="tts-byok-hint-badge"><?php esc_html_e( 'Hint!', 'text-to-speech-tts' ); ?></span>
        <?php if ( 'stack' === $tts_byok_hint_variant ) : ?>
            <strong class="tts-byok-hint-text"><?php esc_html_e( 'Connect your ElevenLabs API key and get 10,000 FREE bonus credits every month.', 'text-to-speech-tts' ); ?></strong>
        <?php else : ?>
            <span class="tts-byok-hint-text"><?php esc_html_e( 'Connect your ElevenLabs API key and get 10,000 FREE bonus credits every month.', 'text-to-speech-tts' ); ?></span>
        <?php endif; ?>
    </div>
    <?php if ( 'stack' === $tts_byok_hint_variant ) : ?>
        <p class="tts-card-desc tts-byok-hint-desc">
            <?php
            printf(
                // translators: %1$s and %2$s wrap the "ElevenLabs account" link.
                esc_html__( 'Create an %1$sElevenLabs account%2$s, add your API key, and your free monthly credits stack on top of your TTSWP credits.', 'text-to-speech-tts' ),
                '<a href="https://try.elevenlabs.io/ttswp" target="_blank" rel="noopener noreferrer" class="tts-byok-link">',
                '</a>'
            );
            ?>
        </p>
    <?php endif; ?>
    <a href="https://app.ttswp.com/settings" target="_blank" rel="noopener noreferrer" class="tts-btn tts-btn-primary">
        <?php esc_html_e( 'Connect ElevenLabs', 'text-to-speech-tts' ); ?>
    </a>
</div>

<?php elseif ( ! empty( $qs_elevenlabs_error ) ) : ?>
<div class="tts-byok-row">
    <div class="tts-byok-row-group">
        <span class="tts-pill tts-pill-error"><?php esc_html_e( 'ElevenLabs key invalid', 'text-to-speech-tts' ); ?></span>
        <span class="tts-byok-note">
            <?php
            printf(
                // translators: %1$s and %2$s wrap the app.ttswp.com link.
                esc_html__( 'Update it on %1$sapp.ttswp.com%2$s.', 'text-to-speech-tts' ),
                '<a href="https://app.ttswp.com/settings" target="_blank" rel="noopener noreferrer">',
                '</a>'
            );
            ?>
        </span>
    </div>
    <a href="https://app.ttswp.com/settings" target="_blank" rel="noopener noreferrer" class="tts-byok-link">
        <?php esc_html_e( 'Update key', 'text-to-speech-tts' ); ?> &rarr;
    </a>
</div>

<?php else : ?>
<div class="tts-byok-row">
    <div class="tts-byok-row-group">
        <span class="tts-pill tts-pill-success"><?php esc_html_e( 'ElevenLabs connected', 'text-to-speech-tts' ); ?></span>
        <?php if ( ! empty( $qs_elevenlabs['tier'] ) ) : ?>
            <span class="tts-byok-tier"><?php echo esc_html( ucfirst( str_replace( '_', ' ', $qs_elevenlabs['tier'] ) ) ); ?></span>
        <?php endif; ?>
    </div>
    <?php if ( isset( $qs_elevenlabs['credits_remaining'] ) && $qs_elevenlabs['credits_remaining'] !== null ) : ?>
    <?php
    // Prefer the raw usage count so overage ("3,094,404 / 10,000") is visible.
    // Older responses without character_count fall back to limit - remaining.
    $qs_el_used = isset( $qs_elevenlabs['character_count'] )
        ? (int) $qs_elevenlabs['character_count']
        : max( 0, (int) ( $qs_elevenlabs['character_limit'] ?? 0 ) - (int) $qs_elevenlabs['credits_remaining'] );
    ?>
    <div class="tts-byok-credits">
        <strong><?php echo esc_html( number_format( $qs_el_used ) ); ?></strong>
        <?php if ( ! empty( $qs_elevenlabs['character_limit'] ) && $qs_elevenlabs['character_limit'] > 0 ) : ?>
            / <?php echo esc_html( number_format( $qs_elevenlabs['character_limit'] ) ); ?>
        <?php endif; ?>
        <span class="tts-byok-credits-label"><?php esc_html_e( 'ElevenLabs credits per month', 'text-to-speech-tts' ); ?></span>
        <?php if ( ! empty( $qs_elevenlabs['next_reset_unix'] ) ) : ?>
            <?php /* translators: %s: Date when the ElevenLabs credit allowance next resets, formatted per the site's date format. */ ?>
            <span class="tts-byok-credits-reset">&nbsp; | &nbsp;<?php printf( esc_html__( 'Resets %s', 'text-to-speech-tts' ), esc_html( wp_date( get_option( 'date_format' ), $qs_elevenlabs['next_reset_unix'] ) ) ); ?></span>
        <?php endif; ?>
    </div>
    <?php endif; ?>
</div>
<?php endif; ?>

<?php
// Reset the variant so the next include starts fresh.
unset( $tts_byok_hint_variant );
