<?php
/**
 * PayStarSea Admin
 * 
 * Admin functionality and order management
 */

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

class SeaXPay_Admin {
    
    public function __construct() {
        add_action('admin_menu', array($this, 'add_admin_menu'));
        add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts'));
        add_action('wp_ajax_seaxpay_test_connection', array($this, 'ajax_test_connection'));
    }
    
    public function add_admin_menu() {
        add_menu_page(
            __('PayStarSea', 'paystarsea-crypto-payment-gateway'),
            __('PayStarSea', 'paystarsea-crypto-payment-gateway'),
            'manage_woocommerce',
            'seaxpay',
            array($this, 'orders_page'),
            'dashicons-money-alt',
            56
        );
        
        add_submenu_page(
            'seaxpay',
            __('Orders', 'paystarsea-crypto-payment-gateway'),
            __('Orders', 'paystarsea-crypto-payment-gateway'),
            'manage_woocommerce',
            'seaxpay',
            array($this, 'orders_page')
        );
        
        add_submenu_page(
            'seaxpay',
            __('Logs', 'paystarsea-crypto-payment-gateway'),
            __('Logs', 'paystarsea-crypto-payment-gateway'),
            'manage_woocommerce',
            'seaxpay-logs',
            array($this, 'logs_page')
        );
        
        add_submenu_page(
            'seaxpay',
            __('Settings', 'paystarsea-crypto-payment-gateway'),
            __('Settings', 'paystarsea-crypto-payment-gateway'),
            'manage_woocommerce',
            'admin.php?page=wc-settings&tab=checkout&section=seaxpay'
        );
    }
    
    public function enqueue_admin_scripts($hook) {
        $is_seaxpay_page = strpos($hook, 'seaxpay') !== false;
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        $is_wc_settings = ($hook === 'woocommerce_page_wc-settings' && isset($_GET['section']) && sanitize_text_field(wp_unslash($_GET['section'])) === 'seaxpay');
        
        if (!$is_seaxpay_page && !$is_wc_settings) {
            return;
        }
        
        wp_enqueue_style('seaxpay-admin', SEAXPAY_PLUGIN_URL . 'assets/css/admin.css', array(), SEAXPAY_VERSION);
        wp_enqueue_script('seaxpay-admin', SEAXPAY_PLUGIN_URL . 'assets/js/admin.js', array('jquery'), SEAXPAY_VERSION, true);
        
        wp_localize_script('seaxpay-admin', 'seaxpayAdmin', array(
            'ajax_url' => admin_url('admin-ajax.php'),
            'nonce' => wp_create_nonce('seaxpay_admin')
        ));
    }
    
    public function orders_page() {
        global $wpdb;
        
        $per_page = 20;
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        $paged = isset($_GET['paged']) ? max(1, absint($_GET['paged'])) : 1;
        $offset = ($paged - 1) * $per_page;
        
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        $status_filter = isset($_GET['status']) ? sanitize_text_field(wp_unslash($_GET['status'])) : '';
        
        if ($status_filter) {
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
            $total = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->prefix}seaxpay_orders WHERE status = %s", $status_filter));
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
            $orders = $wpdb->get_results($wpdb->prepare(
                "SELECT * FROM {$wpdb->prefix}seaxpay_orders WHERE status = %s ORDER BY create_time DESC LIMIT %d OFFSET %d",
                $status_filter, $per_page, $offset
            ));
        } else {
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
            $total = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}seaxpay_orders");
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
            $orders = $wpdb->get_results($wpdb->prepare(
                "SELECT * FROM {$wpdb->prefix}seaxpay_orders ORDER BY create_time DESC LIMIT %d OFFSET %d",
                $per_page, $offset
            ));
        }
        
        include SEAXPAY_PLUGIN_DIR . 'admin/views/orders.php';
    }
    
    public function logs_page() {
        global $wpdb;
        
        $per_page = 50;
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        $paged = isset($_GET['paged']) ? max(1, absint($_GET['paged'])) : 1;
        $offset = ($paged - 1) * $per_page;
        
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        $type_filter = isset($_GET['type']) ? sanitize_text_field(wp_unslash($_GET['type'])) : '';
        
        if ($type_filter) {
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
            $total = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->prefix}seaxpay_logs WHERE log_type = %s", $type_filter));
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
            $logs = $wpdb->get_results($wpdb->prepare(
                "SELECT * FROM {$wpdb->prefix}seaxpay_logs WHERE log_type = %s ORDER BY create_time DESC LIMIT %d OFFSET %d",
                $type_filter, $per_page, $offset
            ));
        } else {
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
            $total = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}seaxpay_logs");
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
            $logs = $wpdb->get_results($wpdb->prepare(
                "SELECT * FROM {$wpdb->prefix}seaxpay_logs ORDER BY create_time DESC LIMIT %d OFFSET %d",
                $per_page, $offset
            ));
        }
        
        include SEAXPAY_PLUGIN_DIR . 'admin/views/logs.php';
    }
    
    public function ajax_test_connection() {
        check_ajax_referer('seaxpay_admin', 'nonce');
        
        if (!current_user_can('manage_woocommerce')) {
            wp_send_json_error(__('Permission denied', 'paystarsea-crypto-payment-gateway'));
        }
        
        $api_client = new SeaXPay_API_Client();
        $result = $api_client->test_connection();
        
        if ($result['success']) {
            wp_send_json_success($result['message']);
        } else {
            wp_send_json_error($result['message']);
        }
    }
}

new SeaXPay_Admin();
