<?php

namespace MovieEngine\Admin\MetaBox;

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

class MovieMetaBox {
    use MetaBoxTrait;

    public function init() {
        add_action('add_meta_boxes', [$this, 'add_meta_boxes']);
        add_action('save_post', [$this, 'save_meta_boxes']);
    }

    public function add_meta_boxes() {
        add_meta_box(
            'movie_engine_movie_tabs',
            esc_html__('Movie Information', 'cine-movie-engine'),
            [$this, 'render_movie_tabs'],
            'movie_engine_movie',
            'normal',
            'high'
        );
    }

    public function render_movie_tabs($post) {
        wp_nonce_field('movie_engine_save_meta', 'movie_engine_meta_nonce');
?>
        <div class="movie-engine-metabox-tabs-wrapper">
            <div class="movie-engine-metabox-tabs-nav">
                <a href="#details" class="movie-engine-tab-link active" data-tab="details">
                    <span class="fa-solid fa-circle-info"></span> <?php esc_html_e('Details', 'cine-movie-engine'); ?>
                </a>
                <a href="#media" class="movie-engine-tab-link" data-tab="media">
                    <span class="fa-solid fa-image"></span> <?php esc_html_e('Media', 'cine-movie-engine'); ?>
                </a>
                <a href="#cast" class="movie-engine-tab-link" data-tab="cast">
                    <span class="fa-solid fa-users"></span> <?php esc_html_e('Cast & Crew', 'cine-movie-engine'); ?>
                </a>
                <a href="#sources" class="movie-engine-tab-link" data-tab="sources">
                    <span class="fa-solid fa-film"></span> <?php esc_html_e('Video Sources', 'cine-movie-engine'); ?>
                </a>
                <a href="#subtitles" class="movie-engine-tab-link" data-tab="subtitles">
                    <span class="fa-solid fa-closed-captioning"></span> <?php esc_html_e('Subtitles', 'cine-movie-engine'); ?>
                </a>
                <a href="#tmdb" class="movie-engine-tab-link" data-tab="tmdb">
                    <span class="fa-solid fa-download"></span> <?php esc_html_e('TMDb Import', 'cine-movie-engine'); ?>
                </a>
                <a href="#affiliate" class="movie-engine-tab-link" data-tab="affiliate">
                    <span class="fa-solid fa-cart-shopping"></span> <?php esc_html_e('Affiliate', 'cine-movie-engine'); ?>
                </a>
                <a href="#access" class="movie-engine-tab-link" data-tab="access">
                    <span class="fa-solid fa-lock"></span> <?php esc_html_e('Access Control', 'cine-movie-engine'); ?>
                </a>
            </div>

            <div class="movie-engine-metabox-tabs-content">
                <div id="tab-details" class="movie-engine-tab-pane active">
                    <?php $this->render_details_content($post); ?>
                </div>

                <div id="tab-media" class="movie-engine-tab-pane">
                    <?php $this->render_media_content($post); ?>
                </div>

                <div id="tab-cast" class="movie-engine-tab-pane">
                    <?php $this->render_cast_content($post); ?>
                </div>

                <div id="tab-sources" class="movie-engine-tab-pane">
                    <?php $this->render_sources_content($post); ?>
                </div>

                <div id="tab-subtitles" class="movie-engine-tab-pane">
                    <?php $this->render_subtitles_content($post); ?>
                </div>

                <div id="tab-tmdb" class="movie-engine-tab-pane">
                    <?php $this->render_tmdb_import_content($post, 'movie'); ?>
                </div>

                <div id="tab-affiliate" class="movie-engine-tab-pane">
                    <?php $this->render_affiliate_content($post); ?>
                </div>

                <div id="tab-access" class="movie-engine-tab-pane">
                    <?php $this->render_access_content($post); ?>
                </div>
            </div>
        </div>
    <?php
    }

    public function render_details_content($post) {
        $runtime      = get_post_meta($post->ID, '_movie_engine_runtime', true);
        $rating       = get_post_meta($post->ID, '_movie_engine_rating', true);
        $trailer      = get_post_meta($post->ID, '_movie_engine_trailer', true);
        $release_date = get_post_meta($post->ID, '_movie_engine_release_date', true);
        $mpaa         = get_post_meta($post->ID, '_movie_engine_mpaa', true);
        $imdb_id      = get_post_meta($post->ID, '_movie_engine_imdb_id', true);
    ?>
        <div class="movie-engine-metabox-grid">
            <div class="movie-engine-field-group">
                <label for="movie_engine_runtime"><span class="fa-solid fa-clock"></span> <?php esc_html_e('Runtime (min)', 'cine-movie-engine'); ?></label>
                <input type="number" id="movie_engine_runtime" name="movie_engine_runtime" value="<?php echo esc_attr($runtime); ?>" placeholder="120">
            </div>
            <div class="movie-engine-field-group">
                <label for="movie_engine_rating"><span class="fa-solid fa-star"></span> <?php esc_html_e('Rating (0-10)', 'cine-movie-engine'); ?></label>
                <input type="text" id="movie_engine_rating" name="movie_engine_rating" value="<?php echo esc_attr($rating); ?>" placeholder="8.5">
            </div>
            <div class="movie-engine-field-group">
                <label for="movie_engine_release_date"><span class="fa-solid fa-calendar-days"></span> <?php esc_html_e('Release Date', 'cine-movie-engine'); ?></label>
                <input type="date" id="movie_engine_release_date" name="movie_engine_release_date" value="<?php echo esc_attr($release_date); ?>">
            </div>
            <div class="movie-engine-field-group">
                <label for="movie_engine_mpaa"><span class="fa-solid fa-triangle-exclamation"></span> <?php esc_html_e('MPAA Rating', 'cine-movie-engine'); ?></label>
                <select id="movie_engine_mpaa" name="movie_engine_mpaa" class="me-select-full">
                    <option value=""><?php esc_html_e('Select Rating', 'cine-movie-engine'); ?></option>
                    <?php
                    $ratings = ['G', 'PG', 'PG-13', 'R', 'NC-17', 'TV-Y', 'TV-G', 'TV-PG', 'TV-14', 'TV-MA'];
                    foreach ($ratings as $r) {
                        echo '<option value="' . esc_attr($r) . '" ' . selected($mpaa, $r, false) . '>' . esc_html($r) . '</option>';
                    }
                    ?>
                </select>
            </div>
            <div class="movie-engine-field-group">
                <label for="movie_engine_imdb_id"><span class="fa-solid fa-circle-info"></span> <?php esc_html_e('IMDb ID', 'cine-movie-engine'); ?></label>
                <input type="text" id="movie_engine_imdb_id" name="movie_engine_imdb_id" value="<?php echo esc_attr($imdb_id); ?>" placeholder="tt1234567">
            </div>
            <div class="movie-engine-field-group full-width">
                <label for="movie_engine_trailer"><span class="fa-solid fa-film"></span> <?php esc_html_e('Trailer URL (YouTube)', 'cine-movie-engine'); ?></label>
                <input type="url" id="movie_engine_trailer" name="movie_engine_trailer" value="<?php echo esc_attr($trailer); ?>" placeholder="https://youtube.com/watch?v=...">
            </div>
        </div>
<?php
    }

    public function render_access_content($post) {
        if (has_action('movie_engine_render_access_metabox')) {
            do_action('movie_engine_render_access_metabox', $post);
            return;
        }

        \MovieEngine\Admin\Upgrade::render_metabox_upgrade_notice(
            esc_html__('Access Control & PPV', 'cine-movie-engine'),
            esc_html__('Take full control over who can watch your content. Set up premium content plans and Pay Per View (PPV) options with ease.', 'cine-movie-engine'),
            [
                esc_html__('Mark as Premium Content', 'cine-movie-engine'),
                esc_html__('Set Sale Price (PPV)', 'cine-movie-engine'),
                esc_html__('Set Rental Price (PPV)', 'cine-movie-engine'),
                esc_html__('Custom Rental Durations', 'cine-movie-engine'),
                esc_html__('Subscription-based Access', 'cine-movie-engine'),
            ]
        );
    }

    public function save_meta_boxes($post_id) {
        if (! isset($_POST['movie_engine_meta_nonce']) || ! wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['movie_engine_meta_nonce'])), 'movie_engine_save_meta')) {
            return;
        }

        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
            return;
        }

        if (! current_user_can('edit_post', $post_id)) {
            return;
        }

        if (get_post_type($post_id) !== 'movie_engine_movie') {
            return;
        }

        if (isset($_POST['movie_engine_runtime'])) {
            update_post_meta($post_id, '_movie_engine_runtime', sanitize_text_field(wp_unslash($_POST['movie_engine_runtime'])));
        }
        if (isset($_POST['movie_engine_rating'])) {
            update_post_meta($post_id, '_movie_engine_rating', sanitize_text_field(wp_unslash($_POST['movie_engine_rating'])));
        }
        if (isset($_POST['movie_engine_trailer'])) {
            update_post_meta($post_id, '_movie_engine_trailer', esc_url_raw(wp_unslash($_POST['movie_engine_trailer'])));
        }
        if (isset($_POST['movie_engine_release_date'])) {
            update_post_meta($post_id, '_movie_engine_release_date', sanitize_text_field(wp_unslash($_POST['movie_engine_release_date'])));
        }
        if (isset($_POST['movie_engine_mpaa'])) {
            update_post_meta($post_id, '_movie_engine_mpaa', sanitize_text_field(wp_unslash($_POST['movie_engine_mpaa'])));
        }
        if (isset($_POST['movie_engine_imdb_id'])) {
            update_post_meta($post_id, '_movie_engine_imdb_id', sanitize_text_field(wp_unslash($_POST['movie_engine_imdb_id'])));
        }

        do_action('movie_engine_save_access_metabox', $post_id);

        $this->save_shared_fields($post_id);
    }
}
