<?php
if ( ! defined( 'ABSPATH' ) ) exit;

require_once CDTV_PLUGIN_DIR . 'admin/class-cdtv-admin.php';
require_once CDTV_PLUGIN_DIR . 'public/class-cdtv-public.php';

/**
 * Core plugin class – wires everything together via the loader.
 */
class CDTV_Core {

    protected $loader;
    protected $plugin_slug = 'codede-table-viewer';

    public function __construct() {
        $this->loader = new CDTV_Loader();
        $this->define_admin_hooks();
        $this->define_public_hooks();
        $this->define_shared_hooks();
    }

    private function define_admin_hooks() {
        $admin = new CDTV_Admin( $this->plugin_slug, CDTV_VERSION );

        $this->loader->add_action( 'admin_enqueue_scripts', $admin, 'enqueue_styles' );
        $this->loader->add_action( 'admin_enqueue_scripts', $admin, 'enqueue_scripts' );
        $this->loader->add_action( 'admin_menu',            $admin, 'add_admin_menu' );
        $this->loader->add_action( 'admin_init',            $admin, 'register_settings' );
        $this->loader->add_action( 'wp_ajax_cdtv_upload_file',       $admin, 'ajax_upload_file' );
        $this->loader->add_action( 'wp_ajax_cdtv_save_table',        $admin, 'ajax_save_table' );
        $this->loader->add_action( 'wp_ajax_cdtv_save_table_meta',   $admin, 'ajax_save_table_meta' );
        $this->loader->add_action( 'wp_ajax_cdtv_get_table_preview', $admin, 'ajax_get_table_preview' );
        $this->loader->add_action( 'wp_ajax_cdtv_delete_table',      $admin, 'ajax_delete_table' );
        $this->loader->add_action( 'wp_ajax_cdtv_export_table',      $admin, 'ajax_export_table' );
        $this->loader->add_action( 'wp_ajax_cdtv_export_xlsx',       $admin, 'ajax_export_xlsx' );
        $this->loader->add_action( 'wp_ajax_cdtv_save_design',        $admin, 'ajax_save_design' );
        $this->loader->add_action( 'wp_ajax_cdtv_get_table_design',   $admin, 'ajax_get_table_design' );
        $this->loader->add_filter( 'plugin_action_links_' . CDTV_PLUGIN_BASENAME, $admin, 'add_action_links' );
    }

    private function define_public_hooks() {
        $public = new CDTV_Public( $this->plugin_slug, CDTV_VERSION );

        $this->loader->add_action( 'wp_enqueue_scripts', $public, 'enqueue_styles' );
        $this->loader->add_action( 'wp_enqueue_scripts', $public, 'enqueue_scripts' );
        $this->loader->add_action( 'init',               $public, 'register_shortcode' );
    }

    private function define_shared_hooks() {
        $this->loader->add_action( 'init', $this, 'register_post_type' );
    }

    public function register_post_type() {
        register_post_type(
            'cdtv_table',
            array(
                'labels'          => array(
                    'name'          => esc_html__( 'Tables', 'codede-table-viewer' ),
                    'singular_name' => esc_html__( 'Table', 'codede-table-viewer' ),
                ),
                'public'          => false,
                'show_ui'         => false,
                'capability_type' => 'post',
                'supports'        => array( 'title' ),
                'rewrite'         => false,
            )
        );
    }

    public function run() {
        $this->loader->run();
    }
}
