<?php
/**
 * Plugin Name: BW Admin Note
 * Plugin URI: https://bowdenworks.com
 * Description: Add internal admin notes to posts and pages, visible only to logged-in users in the editor.
 * Version: 1.0.0
 * Author: Bowden Works
 * Author URI: https://bowdenworks.com
 * License: GPL v2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: bw-admin-note
 */

// Prevent direct access
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

// Plugin constants
define( 'BW_ADMIN_NOTE_VERSION', '1.0.0' );
define( 'BW_ADMIN_NOTE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'BW_ADMIN_NOTE_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'BW_ADMIN_NOTE_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );

/**
 * Main plugin class
 */
class BW_Admin_Note {

    /**
     * Single instance
     */
    private static $instance = null;

    /**
     * Get instance
     */
    public static function get_instance() {
        if ( null === self::$instance ) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Constructor
     */
    private function __construct() {
        $this->load_dependencies();
        $this->init_hooks();
    }

    /**
     * Load required files
     */
    private function load_dependencies() {
        require_once BW_ADMIN_NOTE_PLUGIN_DIR . 'includes/class-bw-admin-note-settings.php';
        require_once BW_ADMIN_NOTE_PLUGIN_DIR . 'includes/class-bw-admin-note-gutenberg.php';
    }

    /**
     * Initialize hooks
     */
    private function init_hooks() {
        // Activation/deactivation
        register_activation_hook( __FILE__, array( $this, 'activate' ) );
        register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );

        // Initialize components
        add_action( 'init', array( $this, 'init' ) );
        add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) );
    }

    /**
     * Initialize plugin components
     */
    public function init() {
        // Initialize settings
        BW_Admin_Note_Settings::get_instance();

        // Initialize Gutenberg support
        BW_Admin_Note_Gutenberg::get_instance();
    }

    /**
     * Enqueue admin assets
     */
    public function enqueue_admin_assets( $hook ) {
        global $post;

        // Only load on post edit screens and settings page
        $load_on = array( 'post.php', 'post-new.php', 'settings_page_bw-admin-note' );
        if ( ! in_array( $hook, $load_on ) ) {
            return;
        }

        // Check if current post type is enabled (for post edit screens)
        if ( isset( $post ) && ! $this->is_post_type_enabled( $post->post_type ) ) {
            return;
        }

        wp_enqueue_style(
            'bw-admin-note-admin',
            BW_ADMIN_NOTE_PLUGIN_URL . 'assets/css/admin.css',
            array(),
            BW_ADMIN_NOTE_VERSION
        );
    }

    /**
     * Check if a post type has admin notes enabled
     */
    public function is_post_type_enabled( $post_type ) {
        $options = get_option( 'bw_admin_note_settings', array() );
        $enabled_types = isset( $options['post_types'] ) ? $options['post_types'] : array( 'page' );
        return in_array( $post_type, $enabled_types );
    }

    /**
     * Plugin activation
     */
    public function activate() {
        // Set default options
        if ( false === get_option( 'bw_admin_note_settings' ) ) {
            add_option( 'bw_admin_note_settings', array(
                'post_types' => array( 'page' ),
            ) );
        }

        // Flush rewrite rules
        flush_rewrite_rules();
    }

    /**
     * Plugin deactivation
     */
    public function deactivate() {
        // Nothing to clean up
    }
}

// Initialize plugin
BW_Admin_Note::get_instance();
