<?php
/**
 * Registers the mediat_task custom post type, mediat_tag taxonomy,
 * and custom post statuses.
 *
 * @package MediaTasker
 */

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

/**
 * Class Mediat_Post_Types
 *
 * Handles all CPT, taxonomy, and status registrations.
 */
class Mediat_Post_Types
{

    /**
     * Hook into WordPress.
     *
     * @return void
     */
    public static function init()
    {
        add_action('init', array(__CLASS__, 'register_post_type'));
        add_action('init', array(__CLASS__, 'register_taxonomy'));
        add_action('init', array(__CLASS__, 'register_post_statuses'));
    }

    /**
     * Register the mediat_task custom post type.
     *
     * @return void
     */
    public static function register_post_type()
    {
        $labels = array(
            'name' => esc_html_x('Tasks', 'Post type general name', 'mediatasker'),
            'singular_name' => esc_html_x('Task', 'Post type singular name', 'mediatasker'),
            'menu_name' => esc_html__('Tasks', 'mediatasker'),
            'add_new' => esc_html__('Add New', 'mediatasker'),
            'add_new_item' => esc_html__('Add New Task', 'mediatasker'),
            'edit_item' => esc_html__('Edit Task', 'mediatasker'),
            'new_item' => esc_html__('New Task', 'mediatasker'),
            'view_item' => esc_html__('View Task', 'mediatasker'),
            'view_items' => esc_html__('View Tasks', 'mediatasker'),
            'search_items' => esc_html__('Search Tasks', 'mediatasker'),
            'not_found' => esc_html__('No tasks found.', 'mediatasker'),
            'not_found_in_trash' => esc_html__('No tasks found in Trash.', 'mediatasker'),
            'all_items' => esc_html__('All Tasks', 'mediatasker'),
            'archives' => esc_html__('Task Archives', 'mediatasker'),
            'attributes' => esc_html__('Task Attributes', 'mediatasker'),
            'insert_into_item' => esc_html__('Insert into task', 'mediatasker'),
            'uploaded_to_this_item' => esc_html__('Uploaded to this task', 'mediatasker'),
            'filter_items_list' => esc_html__('Filter tasks list', 'mediatasker'),
            'items_list_navigation' => esc_html__('Tasks list navigation', 'mediatasker'),
            'items_list' => esc_html__('Tasks list', 'mediatasker'),
        );

        $args = array(
            'labels' => $labels,
            'description' => esc_html__('Media tasks managed by MediaTasker.', 'mediatasker'),
            'public' => false,
            'show_ui' => true,
            'show_in_menu' => false, // We manage the menu ourselves in class-mediat_admin.php.
            'show_in_rest' => true,
            'capability_type' => 'post',
            'has_archive' => false,
            'hierarchical' => false,
            'supports' => array('title', 'editor', 'thumbnail', 'comments'),
            'menu_icon' => 'dashicons-format-gallery',
            'exclude_from_search' => true,
            'rewrite' => false,
        );

        register_post_type('mediat_task', $args);
    }

    /**
     * Register private taxonomies (like mediat_view_source).
     *
     * @return void
     */
    public static function register_taxonomy()
    {
        register_taxonomy('mediat_view_source', 'mediat_task', array(
            'label' => esc_html__('View Source', 'mediatasker'),
            'public' => false,
            'show_ui' => false,
            'show_admin_column' => false,
            'show_in_rest' => false, // Internal only
            'hierarchical' => false,
            'rewrite' => false,
            'query_var' => false,
        ));
    }

    /**
     * Register custom post statuses for task lifecycle.
     *
     * @return void
     */
    public static function register_post_statuses()
    {
        $statuses = self::get_task_statuses();

        foreach ($statuses as $status_key => $data) {
            register_post_status(
                $status_key,
                array(
                    'label' => $data['label'],
                    'public' => true,
                    'exclude_from_search' => false,
                    'show_in_admin_all_list' => true,
                    'show_in_admin_status_list' => true,
                    'label_count' => $data['label_count'],
                )
            );
        }
    }

    /**
     * Returns the defined task statuses.
     *
     * @return array Associative array of status_key => label.
     */
    public static function get_task_statuses()
    {
        return array(
            'mediat_pending' => array(
                'label' => esc_html__('Pending', 'mediatasker'),
                /* translators: %s: Number of tasks in this status. */
                'label_count' => _n_noop('Pending <span class="count">(%s)</span>', 'Pending <span class="count">(%s)</span>', 'mediatasker'),
            ),
            'mediat_in_progress' => array(
                'label' => esc_html__('In Progress', 'mediatasker'),
                /* translators: %s: Number of tasks in this status. */
                'label_count' => _n_noop('In Progress <span class="count">(%s)</span>', 'In Progress <span class="count">(%s)</span>', 'mediatasker'),
            ),
            'mediat_completed' => array(
                'label' => esc_html__('Completed', 'mediatasker'),
                /* translators: %s: Number of tasks in this status. */
                'label_count' => _n_noop('Completed <span class="count">(%s)</span>', 'Completed <span class="count">(%s)</span>', 'mediatasker'),
            ),
            'mediat_approved' => array(
                'label' => esc_html__('Approved', 'mediatasker'),
                /* translators: %s: Number of tasks in this status. */
                'label_count' => _n_noop('Approved <span class="count">(%s)</span>', 'Approved <span class="count">(%s)</span>', 'mediatasker'),
            ),
            'mediat_rejected' => array(
                'label' => esc_html__('Rejected', 'mediatasker'),
                /* translators: %s: Number of tasks in this status. */
                'label_count' => _n_noop('Rejected <span class="count">(%s)</span>', 'Rejected <span class="count">(%s)</span>', 'mediatasker'),
            ),
            'mediat_downloaded' => array(
                'label' => esc_html__('Downloaded', 'mediatasker'),
                /* translators: %s: Number of tasks in this status. */
                'label_count' => _n_noop('Downloaded <span class="count">(%s)</span>', 'Downloaded <span class="count">(%s)</span>', 'mediatasker'),
            ),
            'mediat_not_dl' => array(
                'label' => esc_html__('Not Downloaded', 'mediatasker'),
                /* translators: %s: Number of tasks in this status. */
                'label_count' => _n_noop('Not Downloaded <span class="count">(%s)</span>', 'Not Downloaded <span class="count">(%s)</span>', 'mediatasker'),
            ),
        );
    }
}
