<?php
/**
 * Post Type Manager
 *
 * Manages registration of custom post types for FleekDash.
 * Handles fleekdash_document (Fleek Docs feature) and other custom post types.
 *
 * @package FleekDash V2
 * @since 2.2.0
 */

namespace FleekDash\Core;

defined('ABSPATH') || exit;

class PostTypeManager
{
    /**
     * Option name for Fleek Docs enabled setting
     */
    const FLEEK_DOCS_OPTION = 'fleekdash_fleek_docs_enabled';

    /**
     * Register all custom post types
     *
     * @return void
     */
    public static function register_post_types()
    {
        // Register fleekdash_document post type only if Fleek Docs feature is enabled
        if (self::is_fleek_docs_enabled()) {
            self::register_fleekdash_document();
        }

        // Flush rewrite rules if needed (after CPT registration/deregistration)
        if (\get_option('fleekdash_flush_rewrite_rules', false)) {
            \flush_rewrite_rules();
            \delete_option('fleekdash_flush_rewrite_rules');
        }
    }

    /**
     * Register the fleekdash_document post type
     *
     * @return void
     */
    private static function register_fleekdash_document()
    {
        \register_post_type('fleekdash_document', [
            'labels' => [
                'name' => __('Fleek Docs', 'fleekdash'),
                'singular_name' => __('Document', 'fleekdash'),
                'add_new' => __('Add New Document', 'fleekdash'),
                'add_new_item' => __('Add New Document', 'fleekdash'),
                'edit_item' => __('Edit Document', 'fleekdash'),
                'new_item' => __('New Document', 'fleekdash'),
                'view_item' => __('View Document', 'fleekdash'),
                'search_items' => __('Search Documents', 'fleekdash'),
                'not_found' => __('No documents found', 'fleekdash'),
                'not_found_in_trash' => __('No documents found in trash', 'fleekdash'),
                'all_items' => __('All Documents', 'fleekdash'),
                'menu_name' => __('Fleek Docs', 'fleekdash'),
            ],
            'public' => true,
            'show_ui' => true,
            'show_in_menu' => true,
            'show_in_admin_bar' => true,
            'menu_position' => 20,
            'menu_icon' => 'dashicons-media-document',
            'supports' => ['title', 'editor', 'author', 'revisions', 'custom-fields'],
            'has_archive' => true,
            'rewrite' => ['slug' => 'documents'],
            'show_in_rest' => true,
            'capability_type' => 'post',
        ]);
    }

    /**
     * Check if Fleek Docs feature is enabled
     *
     * @return bool
     */
    public static function is_fleek_docs_enabled(): bool
    {
        return (bool) \get_option(self::FLEEK_DOCS_OPTION, false);
    }

    /**
     * Initialize post type registration hooks
     * Register WordPress action for post type registration
     *
     * @return void
     */
    public static function init()
    {
        \add_action('init', [self::class, 'register_post_types']);
    }
}
