<?php

namespace FleekDash\Controllers\Schema;

/**
 * Schema Helpers
 *
 * Utility methods for schema operations
 *
 * @package FleekDash\Controllers\Schema
 */
class SchemaHelpers
{
    /**
     * Get list of authors
     *
     * @return array Array of author options
     */
    public static function getAuthors()
    {
        $users = get_users(['capability' => 'edit_posts']);
        $authors = [];

        foreach ($users as $user) {
            $authors[] = [
                'value' => $user->ID,
                'label' => $user->display_name
            ];
        }

        return $authors;
    }

    /**
     * Get list of post statuses
     *
     * @return array Array of status options
     */
    public static function getPostStatuses()
    {
        $statuses = \call_user_func('get_post_stati', ['internal' => false], 'objects');
        $status_options = [];

        foreach ($statuses as $status_slug => $status_obj) {
            $status_options[] = [
                'value' => $status_slug,
                'label' => $status_obj->label
            ];
        }

        return $status_options;
    }

    /**
     * Get component from field type
     *
     * @param string $type Field type
     * @return string Component name
     */
    public static function getComponentFromType($type)
    {
        $component_map = [
            'string' => 'input',
            'number' => 'input',
            'integer' => 'input',
            'boolean' => 'checkbox',
            'array' => 'multiselect',
            'textarea' => 'textarea'
        ];

        return $component_map[$type] ?? 'input';
    }

    /**
     * Format field key into readable label
     *
     * @param string $key Field key
     * @return string Formatted label
     */
    public static function formatFieldLabel($key)
    {
        return ucwords(str_replace(['_', '-'], ' ', $key));
    }

    /**
     * Get validation rules from meta config (placeholder — extend when schema-driven rules exist).
     *
     * @return array<string, mixed> Validation rules
     */
    public static function getValidationFromMeta(): array
    {
        return [];
    }

    /**
     * Encode a non-empty array that is not a flat list of numeric scalars (Voxel generic storage).
     *
     * @param array<string|int, mixed> $value
     */
    public static function encodeHeterogeneousVoxelList(array $value): string
    {
        $first = \reset($value);
        $is_assoc_row_repeater = \is_array($first) && $first !== []
            && \array_keys($first) !== \range(0, \count($first) - 1);

        if ($is_assoc_row_repeater) {
            return \json_encode($value);
        }

        return \array_keys($value) === \range(0, \count($value) - 1)
            ? \implode(',', $value)
            : \json_encode($value);
    }

    /**
     * Get excluded WordPress core post types
     *
     * @return array Array of post type slugs to exclude
     */
    public static function getExcludedCorePostTypes()
    {
        return [
            'revision',
            'nav_menu_item',
            'custom_css',
            'customize_changeset',
            'oembed_cache',
            'user_request',
            'wp_block',
            'wp_template',
            'wp_template_part',
            'wp_global_styles',
            'wp_navigation'
        ];
    }

    /**
     * Check if post type should be included in schemas
     *
     * @param object $post_type_obj Post type object
     * @return bool
     */
    public static function shouldIncludePostType($post_type_obj)
    {
        $excluded = self::getExcludedCorePostTypes();

        if (in_array($post_type_obj->name, $excluded)) {
            return false;
        }

        return $post_type_obj->public || $post_type_obj->show_ui;
    }

    /**
     * Register test custom post types for demonstration
     */
    public static function registerTestPostTypes()
    {
        // Register Products
        \register_post_type('product', [
            'label' => 'Products',
            'labels' => [
                'name' => 'Products',
                'singular_name' => 'Product',
                'add_new' => 'Add New Product',
                'add_new_item' => 'Add New Product',
                'edit_item' => 'Edit Product',
                'new_item' => 'New Product',
                'view_item' => 'View Product',
                'view_items' => 'View Products',
            ],
            'description' => 'Test custom post type for products',
            'public' => true,
            'show_ui' => true,
            'show_in_menu' => true,
            'show_in_rest' => true,
            'rest_base' => 'products',
            'supports' => ['title', 'editor', 'excerpt', 'thumbnail', 'custom-fields'],
            'taxonomies' => ['category', 'post_tag'],
            'hierarchical' => false,
        ]);

        // Register Events
        \register_post_type('event', [
            'label' => 'Events',
            'labels' => [
                'name' => 'Events',
                'singular_name' => 'Event',
                'add_new' => 'Add New Event',
                'add_new_item' => 'Add New Event',
                'edit_item' => 'Edit Event',
                'new_item' => 'New Event',
                'view_item' => 'View Event',
                'view_items' => 'View Events',
            ],
            'description' => 'Test custom post type for events',
            'public' => true,
            'show_ui' => true,
            'show_in_menu' => true,
            'show_in_rest' => true,
            'rest_base' => 'events',
            'supports' => ['title', 'editor', 'excerpt', 'thumbnail'],
            'hierarchical' => false,
        ]);
    }
}
