<?php

namespace MovieEngine\Admin\Monetization\Tabs;

if (! defined("ABSPATH")) {
    exit; // Exit if accessed directly
}

class AffiliateTab {

    public static function render($options = [], $standalone = true) {
        if (empty($options)) {
            $options = get_option('movie_engine_monetization_settings', []);
        }
?>
        <?php if ($standalone) : ?>
            <?php settings_fields('movie_engine_monetization'); ?>
            <div class="movie-engine-settings-section">
                <div class="movie-engine-section-header">
                    <h2><?php esc_html_e('Affiliate Links & Partnerships', 'cine-movie-engine'); ?></h2>
                    <p class="description"><?php esc_html_e('Monetize with affiliate links to streaming services, retailers, and ticket vendors', 'cine-movie-engine'); ?></p>
                </div>
            <?php endif; ?>

            <div class="movie-engine-card">
                <div class="movie-engine-card-header">
                    <h3><span class="fa-solid fa-link"></span> <?php esc_html_e('Affiliate Settings', 'cine-movie-engine'); ?></h3>
                </div>
                <div class="movie-engine-card-body">
                    <div class="movie-engine-form-group">
                        <label class="movie-engine-checkbox-label">
                            <input type="hidden" name="movie_engine_monetization_settings[enable_affiliates]" value="0">
                            <input type="checkbox"
                                name="movie_engine_monetization_settings[enable_affiliates]"
                                value="1"
                                <?php checked($options['movie_engine_enable_affiliates'] ?? ($options['enable_affiliates'] ?? 0), 1); ?>>
                            <span><?php esc_html_e('Enable Affiliate Links', 'cine-movie-engine'); ?></span>
                        </label>
                        <p class="movie-engine-help-text"><?php esc_html_e('Show affiliate buttons on movie/series pages', 'cine-movie-engine'); ?></p>
                    </div>

                    <div class="movie-engine-form-group">
                        <label for="button_position"><?php esc_html_e('Button Position', 'cine-movie-engine'); ?></label>
                        <select id="button_position"
                            name="movie_engine_monetization_settings[button_position]"
                            class="movie-engine-select">
                            <?php
                            $button_position = $options['movie_engine_button_position'] ?? ($options['button_position'] ?? 'after_player');
                            ?>
                            <option value="before_player" <?php selected($button_position, 'before_player'); ?>>
                                <?php esc_html_e('Before Player', 'cine-movie-engine'); ?>
                            </option>
                            <option value="after_player" <?php selected($button_position, 'after_player'); ?>>
                                <?php esc_html_e('After Player', 'cine-movie-engine'); ?>
                            </option>
                        </select>
                        <p class="movie-engine-help-text"><?php esc_html_e('Choose where to display affiliate buttons on the player page', 'cine-movie-engine'); ?></p>
                    </div>
                </div>
            </div>

            <div class="movie-engine-card">
                <div class="movie-engine-card-header">
                    <h3><span class="fa-solid fa-cart-shopping"></span> <?php esc_html_e('Amazon Associates', 'cine-movie-engine'); ?></h3>
                </div>
                <div class="movie-engine-card-body">
                    <div class="movie-engine-form-group">
                        <label class="movie-engine-checkbox-label">
                            <input type="hidden" name="movie_engine_monetization_settings[enable_amazon]" value="0">
                            <input type="checkbox"
                                name="movie_engine_monetization_settings[enable_amazon]"
                                value="1"
                                <?php checked($options['movie_engine_enable_amazon'] ?? ($options['enable_amazon'] ?? 0), 1); ?>>
                            <span><?php esc_html_e('Enable Amazon Links', 'cine-movie-engine'); ?></span>
                        </label>
                    </div>

                    <div class="movie-engine-form-group">
                        <label for="amazon_tag"><?php esc_html_e('Amazon Associate Tag', 'cine-movie-engine'); ?></label>
                        <input type="text"
                            id="amazon_tag"
                            name="movie_engine_monetization_settings[amazon_tag]"
                            value="<?php echo esc_attr($options['movie_engine_amazon_tag'] ?? ($options['amazon_tag'] ?? '')); ?>"
                            class="movie-engine-input"
                            placeholder="yourtag-20">
                        <p class="movie-engine-help-text"><?php esc_html_e('Your Amazon Associates tracking ID', 'cine-movie-engine'); ?></p>
                    </div>

                    <div class="movie-engine-form-group">
                        <label for="amazon_button_text"><?php esc_html_e('Button Text', 'cine-movie-engine'); ?></label>
                        <input type="text"
                            id="amazon_button_text"
                            name="movie_engine_monetization_settings[amazon_button_text]"
                            value="<?php echo esc_attr($options['movie_engine_amazon_button_text'] ?? ($options['amazon_button_text'] ?? 'Buy on Amazon')); ?>"
                            class="movie-engine-input">
                    </div>
                </div>
            </div>

            <div class="movie-engine-card">
                <div class="movie-engine-card-header">
                    <h3><span class="fa-solid fa-film"></span> <?php esc_html_e('Streaming Services', 'cine-movie-engine'); ?></h3>
                </div>
                <div class="movie-engine-card-body">
                    <?php
                    $streaming_services = [
                        'netflix' => ['name' => 'Netflix', 'icon' => 'video-alt3'],
                        'amazon_prime' => ['name' => 'Amazon Prime Video', 'icon' => 'video-alt2'],
                        'disney_plus' => ['name' => 'Disney+', 'icon' => 'star-filled'],
                        'hulu' => ['name' => 'Hulu', 'icon' => 'playlist-video'],
                        'hbo_max' => ['name' => 'HBO Max', 'icon' => 'format-video'],
                        'apple_tv' => ['name' => 'Apple TV+', 'icon' => 'tablet'],
                    ];

                    foreach ($streaming_services as $key => $service) :
                    ?>
                        <div class="movie-engine-form-group">
                            <label class="movie-engine-checkbox-label">
                                <input type="hidden" name="movie_engine_monetization_settings[enable_<?php echo esc_attr($key); ?>]" value="0">
                                <input type="checkbox"
                                    name="movie_engine_monetization_settings[enable_<?php echo esc_attr($key); ?>]"
                                    value="1"
                                    <?php checked($options['movie_engine_enable_' . $key] ?? ($options['enable_' . $key] ?? 0), 1); ?>>
                                <span><?php echo esc_html($service['name']); ?></span>
                            </label>
                        </div>

                        <div class="movie-engine-form-group me-ml-30">
                            <label for="<?php echo esc_attr($key); ?>_url"><?php esc_html_e('Affiliate URL Template', 'cine-movie-engine'); ?></label>
                            <input type="url"
                                id="<?php echo esc_attr($key); ?>_url"
                                name="movie_engine_monetization_settings[<?php echo esc_attr($key); ?>_url]"
                                value="<?php echo esc_attr($options['movie_engine_' . $key . '_url'] ?? ($options[$key . '_url'] ?? '')); ?>"
                                class="movie-engine-input"
                                placeholder="https://<?php echo esc_attr($key); ?>.com/ref=YOUR_ID">
                            <p class="movie-engine-help-text"><?php esc_html_e('Use {title} for movie title, {year} for year', 'cine-movie-engine'); ?></p>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>

            <div class="movie-engine-card">
                <div class="movie-engine-card-header">
                    <h3><span class="fa-solid fa-ticket"></span> <?php esc_html_e('Ticket Booking', 'cine-movie-engine'); ?></h3>
                </div>
                <div class="movie-engine-card-body">
                    <div class="movie-engine-form-group">
                        <label class="movie-engine-checkbox-label">
                            <input type="hidden" name="movie_engine_monetization_settings[enable_tickets]" value="0">
                            <input type="checkbox"
                                name="movie_engine_monetization_settings[enable_tickets]"
                                value="1"
                                <?php checked($options['movie_engine_enable_tickets'] ?? ($options['enable_tickets'] ?? 0), 1); ?>>
                            <span><?php esc_html_e('Enable Ticket Booking Links', 'cine-movie-engine'); ?></span>
                        </label>
                    </div>

                    <div class="movie-engine-form-group">
                        <label for="ticket_provider"><?php esc_html_e('Ticket Provider', 'cine-movie-engine'); ?></label>
                        <select id="ticket_provider"
                            name="movie_engine_monetization_settings[ticket_provider]"
                            class="movie-engine-select">
                            <?php
                            $ticket_provider = $options['movie_engine_ticket_provider'] ?? ($options['ticket_provider'] ?? 'fandango');
                            ?>
                            <option value="fandango" <?php selected($ticket_provider, 'fandango'); ?>>Fandango</option>
                            <option value="atom" <?php selected($ticket_provider, 'atom'); ?>>Atom Tickets</option>
                            <option value="movietickets" <?php selected($ticket_provider, 'movietickets'); ?>>MovieTickets.com</option>
                            <option value="custom" <?php selected($ticket_provider, 'custom'); ?>>Custom</option>
                        </select>
                    </div>

                    <div class="movie-engine-form-group">
                        <label for="ticket_affiliate_id"><?php esc_html_e('Affiliate ID', 'cine-movie-engine'); ?></label>
                        <input type="text"
                            id="ticket_affiliate_id"
                            name="movie_engine_monetization_settings[ticket_affiliate_id]"
                            value="<?php echo esc_attr($options['movie_engine_ticket_affiliate_id'] ?? ($options['ticket_affiliate_id'] ?? '')); ?>"
                            class="movie-engine-input">
                    </div>

                    <div class="movie-engine-form-group">
                        <label for="ticket_button_text"><?php esc_html_e('Button Text', 'cine-movie-engine'); ?></label>
                        <input type="text"
                            id="ticket_button_text"
                            name="movie_engine_monetization_settings[ticket_button_text]"
                            value="<?php echo esc_attr($options['movie_engine_ticket_button_text'] ?? ($options['ticket_button_text'] ?? 'Get Tickets')); ?>"
                            class="movie-engine-input">
                    </div>
                </div>
            </div>

            <?php if ($standalone) : ?>
                <div class="movie-engine-form-actions">
                    <?php submit_button(__('Save Changes', 'cine-movie-engine'), 'primary me-btn-primary', 'submit', false); ?>
                    <button type="button" class="button button-link me-reset-section-btn" data-tab="monetization_affiliate"><?php esc_html_e('Reset Section to Defaults', 'cine-movie-engine'); ?></button>
                </div>
            </div>
        <?php endif; ?>
<?php
    }
}
