<?php
/**
 * Text & Dev Tools module.
 *
 * Frontend-driven utility workspace for Markdown, JSON, CSS, color,
 * diff, and encoder tools.
 *
 * @package Noesia_Smart_Waiter
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

if ( ! class_exists( 'Noesia_Swa_Module_Text_Dev_Tools' ) ) {
    /**
     * Free Text & Dev Tools module.
     */
    class Noesia_Swa_Module_Text_Dev_Tools implements Noesia_SWA_Module_Interface {

        /**
         * Unique module identifier.
         *
         * @return string
         */
        public function get_id() {
            return 'text-dev-tools';
        }

        /**
         * Human-readable module label.
         *
         * @return string
         */
        public function get_label() {
            return __( 'Text & Dev Tools', 'noesia-smart-waiter' );
        }

        /**
         * Module tier.
         *
         * @return string
         */
        public function get_tier() {
            return 'free';
        }

        /**
         * Whether the module is enabled.
         *
         * @return bool
         */
        public function is_enabled() {
            return (bool) apply_filters( 'noesia_swa_module_text_dev_tools_enabled', true, $this );
        }

        /**
         * Register module hooks.
         *
         * @return void
         */
        public function register_hooks() {
            add_action( 'noesia_swa_register_admin_sections', array( $this, 'register_admin_section' ) );
            add_filter( 'noesia_swa_admin_i18n', array( $this, 'filter_admin_i18n' ), 10, 2 );

            /**
             * Fires when the Text & Dev Tools module is booted.
             *
             * @param Noesia_Swa_Module_Text_Dev_Tools $this Module instance.
             */
            do_action( 'noesia_swa_module_text_dev_tools_loaded', $this );
        }

        /**
         * Register the admin section.
         *
         * @param mixed $registry Admin sections registry.
         * @return void
         */
        public function register_admin_section( $registry ) {
            if ( ! $registry instanceof Noesia_SWA_Admin_Sections ) {
                return;
            }

            $registry->register_section(
                array(
                    'id'          => 'text-dev-tools',
                    'label'       => $this->get_label(),
                    'nav_label'   => $this->get_label(),
                    'nav_sub'     => __( 'Markdown, JSON, CSS, Diff', 'noesia-smart-waiter' ),
                    'icon'        => 'dashicons-editor-code',
                    'priority'    => 56,
                    'tier'        => 'free',
                    'owner'       => 'text-dev-tools',
                    'is_visible'  => true,
                    'nav_visible' => true,
                    'callback'    => array( $this, 'render_admin' ),
                )
            );
        }

        /**
         * Provide module-owned i18n strings for the frontend-driven UI.
         *
         * @param array<string,string> $i18n Existing payload.
         * @param mixed                $admin Admin instance.
         * @return array<string,string>
         */
        public function filter_admin_i18n( $i18n, $admin ) {
            if ( ! is_array( $i18n ) ) {
                $i18n = array();
            }

            $i18n['text_dev_tools_copy_success']         = __( 'Copied to clipboard.', 'noesia-smart-waiter' );
            $i18n['text_dev_tools_copy_error']           = __( 'Unable to copy right now.', 'noesia-smart-waiter' );
            $i18n['text_dev_tools_json_valid']           = __( 'Valid JSON.', 'noesia-smart-waiter' );
            $i18n['text_dev_tools_json_invalid']         = __( 'Invalid JSON.', 'noesia-smart-waiter' );
            $i18n['text_dev_tools_json_empty']           = __( 'Paste JSON first.', 'noesia-smart-waiter' );
            $i18n['text_dev_tools_markdown_empty']       = __( 'Enter Markdown to generate preview and HTML.', 'noesia-smart-waiter' );
            $i18n['text_dev_tools_diff_empty']           = __( 'Enter text in both panels to compare.', 'noesia-smart-waiter' );
            $i18n['text_dev_tools_base64_invalid']       = __( 'Invalid Base64 input.', 'noesia-smart-waiter' );
            $i18n['text_dev_tools_url_decode_invalid']   = __( 'Invalid URL-encoded input.', 'noesia-smart-waiter' );
            $i18n['text_dev_tools_color_invalid']        = __( 'Enter a valid HEX, RGB, or HSL color.', 'noesia-smart-waiter' );
            $i18n['text_dev_tools_contrast_white']       = __( 'Contrast on white', 'noesia-smart-waiter' );
            $i18n['text_dev_tools_contrast_black']       = __( 'Contrast on black', 'noesia-smart-waiter' );
            $i18n['text_dev_tools_wcag_pass']            = __( 'Pass', 'noesia-smart-waiter' );
            $i18n['text_dev_tools_wcag_fail']            = __( 'Fail', 'noesia-smart-waiter' );
            $i18n['text_dev_tools_best_text']            = __( 'Best text color', 'noesia-smart-waiter' );
            $i18n['text_dev_tools_palette_base']         = __( 'Base', 'noesia-smart-waiter' );
            $i18n['text_dev_tools_palette_tint']         = __( 'Tint', 'noesia-smart-waiter' );
            $i18n['text_dev_tools_palette_shade']        = __( 'Shade', 'noesia-smart-waiter' );
            $i18n['text_dev_tools_diff_identical']       = __( 'No differences found with the current comparison options.', 'noesia-smart-waiter' );
            $i18n['text_dev_tools_markdown_preview']     = __( 'Live preview', 'noesia-smart-waiter' );
            $i18n['text_dev_tools_generated_html']       = __( 'Generated HTML', 'noesia-smart-waiter' );

            return $i18n;
        }

        /**
         * Render the module-local admin view.
         *
         * @return void
         */
        public function render_admin() {
            $module_view = trailingslashit( __DIR__ ) . 'views/admin.php';

            if ( file_exists( $module_view ) ) {
                include $module_view;
                return;
            }

            echo '<div class="noesia-swa-card noesia-swa-card--span2">';
            echo '<div class="noesia-swa-card-header"><div>';
            echo '<h3>' . esc_html__( 'Text & Dev Tools', 'noesia-smart-waiter' ) . '</h3>';
            echo '<p class="noesia-swa-muted">' . esc_html__( 'Module view missing.', 'noesia-smart-waiter' ) . '</p>';
            echo '</div></div></div>';
        }
    }
}
