<?php
/**
 * Launchmind Dashboard (modern admin UI)
 */

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

class Launchmind_Dashboard {

    public static function init() {
        add_action('admin_menu', array(__CLASS__, 'add_menu'));
        add_action('admin_enqueue_scripts', array(__CLASS__, 'enqueue_assets'));
        add_action('wp_ajax_launchmind_get_post', array(__CLASS__, 'ajax_get_post'));
        add_action('wp_ajax_launchmind_get_posts_preview', array(__CLASS__, 'ajax_get_posts_preview'));
    }

    public static function add_menu() {
        add_menu_page(
            __('Launchmind Blog', 'launchmind-blog'),
            __('Launchmind', 'launchmind-blog'),
            'manage_options',
            'launchmind-dashboard',
            array(__CLASS__, 'render_dashboard'),
            'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjQiIGhlaWdodD0iMjQiIHZpZXdCb3g9IjAgMCAyNCAyNCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48cGF0aCBkPSJNMTIgMkwyIDdsIDEwIDUgMTAtNS0xMC01ek0yIDE3bDEwIDUgMTAtNU0yIDEybDEwIDUgMTAtNSIgc3Ryb2tlPSIjMTBiOTgxIiBzdHJva2Utd2lkdGg9IjIiLz48L3N2Zz4=',
            26
        );
    }

    public static function enqueue_assets($hook) {
        if ($hook !== 'toplevel_page_launchmind-dashboard') {
            return;
        }

        wp_enqueue_style(
            'launchmind-dashboard',
            LAUNCHMIND_BLOG_PLUGIN_URL . 'admin/css/dashboard.css',
            array(),
            LAUNCHMIND_BLOG_VERSION
        );

        wp_enqueue_script(
            'launchmind-dashboard',
            LAUNCHMIND_BLOG_PLUGIN_URL . 'admin/js/dashboard.js',
            array('jquery'),
            LAUNCHMIND_BLOG_VERSION,
            true
        );

        wp_localize_script('launchmind-dashboard', 'launchmindDashboard', array(
            'ajaxUrl' => admin_url('admin-ajax.php'),
            'nonce' => wp_create_nonce('launchmind_admin'),
            'strings' => array(
                'loading' => __('Loading...', 'launchmind-blog'),
                'error' => __('Something went wrong.', 'launchmind-blog'),
            ),
        ));
    }

    public static function render_dashboard() {
        require LAUNCHMIND_BLOG_PLUGIN_DIR . 'admin/views/dashboard.php';
    }

    public static function ajax_get_post() {
        check_ajax_referer('launchmind_admin', 'nonce');
        if (!current_user_can('manage_options')) {
            wp_send_json_error(array('message' => __('Permission denied', 'launchmind-blog')));
        }

        $slug = isset($_GET['slug']) ? sanitize_title(wp_unslash($_GET['slug'])) : '';
        $lang = isset($_GET['lang']) ? sanitize_text_field(wp_unslash($_GET['lang'])) : null;

        if (empty($slug)) {
            wp_send_json_error(array('message' => __('Missing slug.', 'launchmind-blog')));
        }

        $post = Launchmind_API::get_post($slug, $lang);

        if (is_wp_error($post) || empty($post)) {
            wp_send_json_error(array('message' => __('Post not found.', 'launchmind-blog')));
        }

        wp_send_json_success(array('post' => $post));
    }

    public static function ajax_get_posts_preview() {
        check_ajax_referer('launchmind_admin', 'nonce');
        if (!current_user_can('edit_posts')) {
            wp_send_json_error(array('message' => __('Permission denied', 'launchmind-blog')));
        }

        $limit = isset($_GET['limit']) ? absint($_GET['limit']) : 6;
        $posts = Launchmind_API::get_posts(array('limit' => $limit));
        if (is_wp_error($posts)) {
            wp_send_json_error(array('message' => $posts->get_error_message()));
        }

        wp_send_json_success(array('posts' => $posts));
    }
}

// Bootstrap
Launchmind_Dashboard::init();
