<?php

namespace MovieEngine\Frontend\Users;

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

class UserDashboard {

    public function __construct() {
        add_shortcode('movie_engine_dashboard', [$this, 'render_dashboard']);
        add_action('wp_ajax_movie_engine_check_session', [$this, 'ajax_check_session']);
        add_action('wp_ajax_nopriv_movie_engine_check_session', [$this, 'ajax_check_session']);
    }

    public function render_dashboard($atts) {
        if (!is_user_logged_in()) {
            return $this->render_guest_view();
        }

        $user_id = get_current_user_id();
        $user    = get_userdata($user_id);
        $is_premium = apply_filters('movie_engine_is_premium', false, $user_id);
        $stats      = \MovieEngine\Frontend\Users\WatchHistory::get_watch_stats($user_id);
        $watchlist_count = \MovieEngine\Frontend\Users\UserLibrary::get_watchlist_count($user_id);
        $transactions    = apply_filters('movie_engine_user_transactions', [], $user_id, 3);

        $design_settings = get_option('movie_engine_design_settings', []);
        $per_page = isset($design_settings['archive_items_per_page']) ? max(1, intval($design_settings['archive_items_per_page'])) : 10;
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        $ref_page = isset($_GET['ref_page']) ? max(1, intval($_GET['ref_page'])) : 1;
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        $pay_page = isset($_GET['pay_page']) ? max(1, intval($_GET['pay_page'])) : 1;

        $ref_count = apply_filters('movie_engine_referral_count', 0, $user_id);
        $pay_count = apply_filters('movie_engine_payout_count', 0, $user_id);


        if (($ref_page - 1) * $per_page >= $ref_count && $ref_count > 0) {
            $ref_page = 1;
        }
        if (($pay_page - 1) * $per_page >= $pay_count && $pay_count > 0) {
            $pay_page = 1;
        }

        $ref_offset = ($ref_page - 1) * $per_page;
        $pay_offset = ($pay_page - 1) * $per_page;

        $referral_stats  = apply_filters('movie_engine_referral_stats', [], $user_id);

        $referral_history = apply_filters('movie_engine_referral_history', [], $user_id, $per_page, $ref_offset);

        $payout_history   = apply_filters('movie_engine_payout_history', [], $user_id, $per_page, $pay_offset);

        ob_start();
        \MovieEngine\Utils\TemplateLoader::get_template_part('dashboard/main', '', [
            'user'            => $user,
            'is_premium'      => $is_premium,
            'stats'           => $stats,
            'watchlist_count' => $watchlist_count,
            'transactions'    => $transactions,
            'referral_stats'  => $referral_stats,
            'referral_history' => $referral_history,
            'referral_pagination' => [
                'current' => $ref_page,
                'total'   => ceil($ref_count / $per_page)
            ],
            'payout_history'   => $payout_history,
            'payout_pagination' => [
                'current' => $pay_page,
                'total'   => ceil($pay_count / $per_page)
            ]
        ]);
        return ob_get_clean();
    }

    private function render_guest_view() {
        $auth = new Auth();
        return $auth->render_login_form([]);
    }

    public function ajax_check_session() {
        wp_send_json_success([
            'logged_in' => is_user_logged_in(),
            'user_id' => get_current_user_id()
        ]);
    }
}
