<?php

/**
 * AP Family Archive - Core Plugin Class
 *
 * Orchestrates all hooks. This plugin is a DISPLAY/publication layer over the
 * shared ANCP Core (spec-02): the site-builder register of the suite. It owns
 * only display/config state (options in 0.1.0; apfaar_ tables only when a
 * feature demands one). All genealogical data lives in the CORE tables and is
 * reached through the core API and read-only queries against core tables -
 * research workflow belongs to AP Family Research, embeds to AP Family Trees.
 *
 * @package    APFAAR_Family_Archive
 * @subpackage APFAAR_Family_Archive/includes
 * @since      0.1.0
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
/**
 * Core plugin class.
 *
 * @since 0.1.0
 */
class APFAAR {
    /**
     * Hook loader.
     *
     * @var APFAAR_Loader
     */
    protected $loader;

    /**
     * Plugin slug.
     *
     * @var string
     */
    protected $plugin_name;

    /**
     * Plugin version.
     *
     * @var string
     */
    protected $version;

    /**
     * Constructor.
     *
     * @since 0.1.0
     */
    public function __construct() {
        $this->plugin_name = 'ap-family-archive';
        $this->version = ( defined( 'APFAAR_VERSION' ) ? APFAAR_VERSION : '0.1.0' );
        $this->load_dependencies();
        $this->define_admin_hooks();
        $this->define_public_hooks();
    }

    /**
     * Load all required files.
     *
     * The core data-access layer (Tables, core table classes) is loaded by
     * the ANCP Core Library on boot (plugins_loaded priority 1), so it is
     * available to every hook callback. Only the plugin-owned display layer
     * is loaded here.
     *
     * @since  0.1.0
     * @return void
     */
    private function load_dependencies() {
        require_once APFAAR_DIR . 'includes/class-apfa-loader.php';
        require_once APFAAR_DIR . 'includes/class-apfa-privacy.php';
        require_once APFAAR_DIR . 'includes/class-apfa-people.php';
        require_once APFAAR_DIR . 'includes/class-apfa-rewrites.php';
        require_once APFAAR_DIR . 'admin/class-apfa-admin.php';
        require_once APFAAR_DIR . 'admin/class-apfa-admin-people.php';
        require_once APFAAR_DIR . 'admin/class-apfa-admin-families.php';
        require_once APFAAR_DIR . 'admin/class-apfa-admin-data.php';
        require_once APFAAR_DIR . 'admin/class-apfa-admin-sources.php';
        require_once APFAAR_DIR . 'admin/class-apfa-admin-media.php';
        require_once APFAAR_DIR . 'admin/class-apfa-admin-branches.php';
        require_once APFAAR_DIR . 'public/class-apfa-public.php';
        require_once APFAAR_DIR . 'public/class-apfa-reports.php';
        $this->loader = new APFAAR_Loader();
    }

    /**
     * Register all public-facing hooks.
     *
     * @since  0.1.0
     * @return void
     */
    private function define_public_hooks() {
        $plugin_public = new APFAAR_Public();
        $plugin_reports = new APFAAR_Reports($plugin_public);
        $plugin_rewrites = new APFAAR_Rewrites();
        $this->loader->add_action( 'init', $plugin_public, 'register_shortcodes' );
        $this->loader->add_action( 'init', $plugin_reports, 'register_shortcodes' );
        // Pretty permalinks for record pages: rules every request, lazy flush
        // (admin) whenever the needed rule set changes.
        $this->loader->add_filter( 'query_vars', $plugin_rewrites, 'register_query_vars' );
        $this->loader->add_action( 'init', $plugin_rewrites, 'add_rules' );
        $this->loader->add_action(
            'init',
            $plugin_rewrites,
            'maybe_flush',
            20
        );
    }

    /**
     * Register all admin-facing hooks.
     *
     * @since  0.1.0
     * @return void
     */
    private function define_admin_hooks() {
        $plugin_admin = new APFAAR_Admin($this->plugin_name, $this->version);
        $this->loader->add_action( 'admin_menu', $plugin_admin, 'register_menu' );
        $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
        // Form handlers (admin-post endpoints; each checks capability + nonce).
        $this->loader->add_action( 'admin_post_apfaar_save_settings', $plugin_admin, 'handle_save_settings' );
        $this->loader->add_action( 'admin_post_apfaar_create_pages', $plugin_admin, 'handle_create_pages' );
        $this->loader->add_action( 'admin_post_apfaar_mw_add', $plugin_admin, 'handle_mw_add' );
        $this->loader->add_action( 'admin_post_apfaar_mw_delete', $plugin_admin, 'handle_mw_delete' );
        $this->loader->add_action( 'admin_post_apfaar_person_save', $plugin_admin, 'handle_person_save' );
        $this->loader->add_action( 'admin_post_apfaar_person_delete', $plugin_admin, 'handle_person_delete' );
        $this->loader->add_action( 'admin_post_apfaar_family_save', $plugin_admin, 'handle_family_save' );
        $this->loader->add_action( 'admin_post_apfaar_family_delete', $plugin_admin, 'handle_family_delete' );
        $this->loader->add_action( 'admin_post_apfaar_child_add', $plugin_admin, 'handle_child_add' );
        $this->loader->add_action( 'admin_post_apfaar_child_remove', $plugin_admin, 'handle_child_remove' );
        $this->loader->add_action( 'admin_post_apfaar_import', $plugin_admin, 'handle_import' );
        // Basic GEDCOM export is a free core service (the user's own factual data
        // at full fidelity); registered unconditionally.
        $this->loader->add_action( 'admin_post_apfaar_export', $plugin_admin, 'handle_export' );
        $this->loader->add_action( 'admin_post_apfaar_source_save', $plugin_admin, 'handle_source_save' );
        $this->loader->add_action( 'admin_post_apfaar_source_delete', $plugin_admin, 'handle_source_delete' );
        $this->loader->add_action( 'admin_post_apfaar_repo_save', $plugin_admin, 'handle_repo_save' );
        $this->loader->add_action( 'admin_post_apfaar_repo_delete', $plugin_admin, 'handle_repo_delete' );
        $this->loader->add_action( 'admin_post_apfaar_cite_add', $plugin_admin, 'handle_cite_add' );
        $this->loader->add_action( 'admin_post_apfaar_cite_delete', $plugin_admin, 'handle_cite_delete' );
        $this->loader->add_action( 'admin_post_apfaar_media_save', $plugin_admin, 'handle_media_save' );
        $this->loader->add_action( 'admin_post_apfaar_media_delete', $plugin_admin, 'handle_media_delete' );
        $this->loader->add_action( 'admin_post_apfaar_media_link', $plugin_admin, 'handle_media_link' );
        $this->loader->add_action( 'admin_post_apfaar_media_unlink', $plugin_admin, 'handle_media_unlink' );
        $this->loader->add_action( 'admin_post_apfaar_media_defphoto', $plugin_admin, 'handle_media_defphoto' );
        // Branch management is FREE (data management everywhere); only the
        // public branch-scoped display is the extension package.
        $this->loader->add_action( 'admin_post_apfaar_branch_save', $plugin_admin, 'handle_branch_save' );
        $this->loader->add_action( 'admin_post_apfaar_branch_apply', $plugin_admin, 'handle_branch_apply' );
        $this->loader->add_action( 'admin_post_apfaar_branch_delete', $plugin_admin, 'handle_branch_delete' );
    }

    /**
     * Run the loader to register all hooks with WordPress.
     *
     * @since  0.1.0
     * @return void
     */
    public function run() {
        $this->loader->run();
    }

}
