<?php
/**
 * Admin-facing functionality.
 *
 * @package    Edocman_Lite
 * @subpackage Edocman_Lite/admin
 */

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

class Edocman_Lite_Admin {

    /**
     * @var string
     */
    private $plugin_name;

    /**
     * @var string
     */
    private $version;

    /**
     * @var Edocman_Lite_Database
     */
    private $database;

    /**
     * @var Edocman_Lite_Admin_Configuration|null
     */
    private $configuration_handler;

    /**
     * @var Edocman_Lite_Admin_Categories|null
     */
    private $categories_handler;

    /**
     * @var Edocman_Lite_Admin_Documents|null
     */
    private $documents_handler;

    /**
     * Constructor.
     *
     * @param string $plugin_name
     * @param string $version
     */
    public function __construct( $plugin_name, $version ) {
        $this->plugin_name = $plugin_name;
        $this->version     = $version;
        $this->database    = new Edocman_Lite_Database();
    }

    /**
     * Enqueue admin CSS.
     */
    public function enqueue_styles() {
        wp_enqueue_style(
            $this->plugin_name,
            EDOCMAN_LITE_PLUGIN_URL . 'admin/css/admin.css',
            array(),
            $this->version,
            'all'
        );
    }

    /**
     * Enqueue admin JS.
     */
    public function enqueue_scripts() {
        wp_enqueue_script(
            $this->plugin_name,
            EDOCMAN_LITE_PLUGIN_URL . 'admin/js/admin.js', // ✅ Sửa: assets/js/ → admin/js/
            array( 'jquery' ),
            $this->version,
            true // ✅ Sửa: false → true (load ở footer)
        );

        wp_localize_script(
            $this->plugin_name,
            'edocman_lite_ajax',
            array(
                'ajax_url' => admin_url( 'admin-ajax.php' ),
                'nonce'    => wp_create_nonce( 'edocman_lite_nonce' ),
            )
        );
    }

    /**
     * Enqueue WP media scripts — only on plugin pages.
     *
     * @param string $hook_suffix
     */
    public function enqueue_admin_media_scripts( $hook_suffix ) {
        if ( false === strpos( $hook_suffix, 'edocman-lite' ) ) {
            return;
        }

        wp_enqueue_media();
        // ✅ Sửa: bỏ wp_enqueue_script('jquery') — WordPress tự quản lý jQuery.

        wp_enqueue_script(
            'edocman-lite-media-uploader',
            EDOCMAN_LITE_PLUGIN_URL . 'admin/js/media-uploader.js',
            array( 'jquery', 'media-upload', 'media-views' ),
            $this->version,
            true
        );
    }

    /**
     * Register admin menu pages.
     */
    public function add_plugin_admin_menu() {
        add_menu_page(
            __( 'Edocman Lite', 'edocman-lite' ),
            __( 'Edocman Lite', 'edocman-lite' ),
            'manage_options',
            'edocman-lite-main',
            array( $this, 'display_configuration_page' ),
            'dashicons-media-document',
            30
        );

        add_submenu_page(
            'edocman-lite-main',
            __( 'Configuration', 'edocman-lite' ),
            __( 'Configuration', 'edocman-lite' ),
            'manage_options',
            'edocman-lite-configuration',
            array( $this, 'display_configuration_page' )
        );

        add_submenu_page(
            'edocman-lite-main',
            __( 'Categories', 'edocman-lite' ),
            __( 'Categories', 'edocman-lite' ),
            'manage_options',
            'edocman-lite-categories',
            array( $this, 'display_categories_page' )
        );

        add_submenu_page(
            'edocman-lite-main',
            __( 'Documents', 'edocman-lite' ),
            __( 'Documents', 'edocman-lite' ),
            'manage_options',
            'edocman-lite-documents',
            array( $this, 'display_documents_page' )
        );

        // Remove duplicate top-level menu item from submenu list.
        remove_submenu_page( 'edocman-lite-main', 'edocman-lite-main' );
    }

    // -------------------------------------------------------------------------
    // Handler setters
    // -------------------------------------------------------------------------

    /**
     * @param Edocman_Lite_Admin_Configuration $handler
     */
    public function set_configuration_handler( $handler ) {
        $this->configuration_handler = $handler;
    }

    /**
     * @param Edocman_Lite_Admin_Categories $handler
     */
    public function set_categories_handler( $handler ) {
        $this->categories_handler = $handler;
    }

    /**
     * @param Edocman_Lite_Admin_Documents $handler
     */
    public function set_documents_handler( $handler ) {
        $this->documents_handler = $handler;
    }

    // -------------------------------------------------------------------------
    // Page display callbacks
    // -------------------------------------------------------------------------

    public function display_configuration_page() {
        if ( $this->configuration_handler ) {
            $this->configuration_handler->display_configuration_page();
        }
    }

    public function display_categories_page() {
        if ( $this->categories_handler ) {
            $this->categories_handler->display_categories_page();
        }
    }

    public function display_documents_page() {
        if ( $this->documents_handler ) {
            $this->documents_handler->display_documents_page();
        }
    }

    // -------------------------------------------------------------------------
    // AJAX handlers
    // -------------------------------------------------------------------------

    /**
     * AJAX: delete a document (admin).
     */
    public function ajax_delete_document() {
        check_ajax_referer( 'edocman_lite_nonce', 'nonce' );

        if ( ! current_user_can( 'manage_options' ) ) {
            wp_send_json_error( array( 'message' => __( 'Permission denied', 'edocman-lite' ) ) );
            return;
        }

        $document_id = isset( $_POST['document_id'] ) ? absint( $_POST['document_id'] ) : 0;

        if ( $document_id <= 0 ) {
            wp_send_json_error( array( 'message' => __( 'Invalid document ID', 'edocman-lite' ) ) );
            return;
        }

        $result = $this->database->delete_document( $document_id );

        if ( $result ) {
            wp_send_json_success( array( 'message' => __( 'Document deleted successfully', 'edocman-lite' ) ) );
        } else {
            wp_send_json_error( array( 'message' => __( 'Failed to delete document', 'edocman-lite' ) ) );
        }
    }

    /**
     * AJAX: delete a category (admin).
     */
    public function ajax_delete_category() {
        check_ajax_referer( 'edocman_lite_nonce', 'nonce' );

        if ( ! current_user_can( 'manage_options' ) ) {
            wp_send_json_error( array( 'message' => __( 'Permission denied', 'edocman-lite' ) ) );
            return;
        }

        $category_id = isset( $_POST['category_id'] ) ? absint( $_POST['category_id'] ) : 0;

        if ( $category_id <= 0 ) {
            wp_send_json_error( array( 'message' => __( 'Invalid category ID', 'edocman-lite' ) ) );
            return;
        }

        $result = $this->database->delete_category( $category_id );

        if ( $result ) {
            wp_send_json_success( array( 'message' => __( 'Category deleted successfully', 'edocman-lite' ) ) );
        } else {
            wp_send_json_error( array( 'message' => __( 'Failed to delete category', 'edocman-lite' ) ) );
        }
    }
}
