<?php
/**
 * License Key Component.
 */
if ( ! function_exists( 'render_license_key_component' ) ) {
    function render_license_key_component() {
        $user_info  = PostologySettings::get_user_from_website();

        $status = "Invalid or Expired";
        $website   = site_url();
        ?>
        <div class="postology-field-wrapper custom-card">
            <h3 style="margin:0;margin-block-end: 21px;color:grey;">License Key</h3>
            <section style="display:flex;flex-direction:column;gap:6px;">
                <label style="display:flex;flex-direction:column;">
                    <input type="password" name="" placeholder="your license key goes here" style="border:none; border-bottom: 1px solid #d3d3d3; inline-size:100%;border-radius:0;outline:0;"/>
                    <span>Install Postology Pro and enter your license key to unlock all features.</span>
                </label>
                <strong class="license-status" style="padding:9px; border-radius:6px;background: #ff5b5b7a;">
                    License Status: <?php echo $status; ?>
                </strong>
            </section>
            <section class="license-key-details-section" style="display:flex;font-size:18px;margin-block-start:6px;">
                <p style="display:flex;flex-direction:column;inline-size:100%;">
                    <strong><?php echo esc_html__( 'Email', 'postology' ); ?></strong>
                    <span><?php echo esc_html( $user_info['email'] ); ?></span>
                </p>
                <p style="display:flex;flex-direction:column;inline-size:100%;">
                    <strong><?php echo esc_html__( 'Website', 'postology' ); ?></strong>
                    <a href="<?php echo esc_url( $website ); ?>" target="_blank"><?php echo esc_html( $website ); ?></a>
                </p>
            </section>
        </div>
        <div class="postology-field-wrapper custom-card">
            <h3 style="margin:0;margin-block-end: 21px;color:grey;">Buy Credits</h3>
            <section style="display:flex;gap:12px;background:#1063ef11;padding-inline:15px;border-radius:6px;">
                <svg style="inline-size: 18px;" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <circle cx="12" cy="12" r="10" stroke="#1063ef" stroke-width="1.5"></circle>
                    <path d="M12 6V18" stroke="#1063ef" stroke-width="1.5" stroke-linecap="round"></path>
                    <path d="M15 9.5C15 8.11929 13.6569 7 12 7C10.3431 7 9 8.11929 9 9.5C9 10.8807 10.3431 12 12 12C13.6569 12 15 13.1193 15 14.5C15 15.8807 13.6569 17 12 17C10.3431 17 9 15.8807 9 14.5" stroke="#1063ef" stroke-width="1.5" stroke-linecap="round"></path>
                </svg>
                <p style="display:flex;flex-direction:column;color:black;">
                    <strong><?php echo esc_html__( 'Available Credits', 'postology' ); ?></strong>
                    <strong style="font-size:30px;"><?php echo esc_html( $user_info['credits'] ); ?></strong>
                </p>
            </section>
            <p><?php echo esc_html__( 'Need more credits? Purchase a plan below:', 'postology' ); ?></p>
            <div style="display:flex;inline-size:100%;gap:12px;">
                <?php
                    $user_email = $user_info['email'];
                    $plans = array(
                        'Basic Plan'   => array(
                            'price'       => 5,
                            'credits'     => "2,500",
                            'description' => "Perfect for small websites getting started with Postology.",
                            'url'         => "https://buy.stripe.com/bIY01FeSn24Q38AeUU"
                        ),
                        'Plus Plan'    => array(
                            'price'       => 19,
                            'credits'     => "11,000",
                            'description' => "Ideal for growing blogs with moderate content needs.",
                            'url'         => "https://buy.stripe.com/cN201Fh0v8teaB2147"
                        ),
                        'Premium Plan' => array(
                            'price'       => 49,
                            'credits'     => "30,000",
                            'description' => "Best for professional content creators and businesses.",
                            'url'         => "https://buy.stripe.com/dR6bKn7pV38UcJaaEG"
                        ),
                        'Ultimate Plan'=> array(
                            'price'       => 99,
                            'credits'     => "105,000",
                            'description' => "Unlimited features for high-volume publishing platforms.",
                            'url'         => "https://buy.stripe.com/aEU5lZ6lRdNyeRi5kl"
                        ),
                    );
                    foreach ( $plans as $plan_name => $details ) {
                        ?>
                        <section style="inline-size:100%;display:flex;flex-direction:column;justify-content: space-between;gap:12px;border-radius:6px;font-size:15px;border:solid 1px lightgrey;min-inline-size:240px;padding: 15px;">
                            <strong style="font-weight:700;font-size:18px;"><?php echo esc_html( $plan_name ); ?> (<?php echo esc_html( $details['credits'] ); ?> credits)</strong>
                            <strong style="color:grey;"><span style="font-weight:700;font-size:21px;color:black;">$<?php echo esc_html( $details['price'] ); ?></span> USD</strong>
                            <p style="font-weight:400;font-size:16px;"><?php echo esc_html( $details['description'] ); ?></p>
                            <a style="border-radius: 6px;color:white; background:#1063ef;text-decoration:none; text-align:center;padding:9px;" href="<?php echo esc_url( $details['url'] . "?prefilled_email={$user_email}" ); ?>" target="_blank">
                                <?php esc_html_e( 'Buy Now', 'postology' ); ?>
                            </a>
                        </section>
                        <?php
                    }
                ?>
            </div>
        </div>
        <?php
    }
}
