<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }

class Falcify_Support {

    public static function init() {
        add_action( 'wp_ajax_falcify_submit_ticket', array( __CLASS__, 'ajax_submit' ) );
    }

    public static function collect_diagnostics() {
        global $wp_version;
        $theme = wp_get_theme();
        $active_plugins = array();
        if ( function_exists( 'get_option' ) ) {
            $list = (array) get_option( 'active_plugins', array() );
            foreach ( $list as $p ) { $active_plugins[] = $p; }
        }
        $opts = Falcify_Settings::get();
        return array(
            'plugin_version'   => FALCIFY_VERSION,
            'wordpress_version'=> $wp_version,
            'php_version'      => PHP_VERSION,
            'site_url'         => home_url(),
            'admin_email'      => get_option( 'admin_email' ),
            'language'         => get_locale(),
            'theme'            => $theme ? ( $theme->get( 'Name' ) . ' ' . $theme->get( 'Version' ) ) : 'unknown',
            'multisite'        => is_multisite(),
            'active_plugins'   => array_values( array_slice( $active_plugins, 0, 30 ) ),
            'falcify_settings' => array(
                'auto_inject'      => ! empty( $opts['auto_inject'] ),
                'builder_mode'     => ! empty( $opts['builder_mode'] ),
                'high_contrast'    => ! empty( $opts['high_contrast'] ),
                'floating_button'  => ! empty( $opts['floating_button'] ),
                'strict_mode'      => ! empty( $opts['strict_mode'] ),
                'enabled_post_types' => $opts['enabled_post_types'] ?? array(),
            ),
            'submitted_at'     => current_time( 'mysql' ),
        );
    }

    public static function ajax_submit() {
        check_ajax_referer( 'falcify_admin', 'nonce' );
        if ( ! current_user_can( 'manage_options' ) ) {
            wp_send_json_error( array( 'message' => esc_html__( 'Action non autorisée.', 'falcify-connector' ) ), 403 );
        }
        $api_key = trim( (string) Falcify_Settings::get_value( 'api_key' ) );
        if ( $api_key === '' ) {
            wp_send_json_error( array( 'error' => esc_html__( 'Configurez d\'abord votre clé API Falcify.', 'falcify-connector' ) ), 400 );
        }
        $subject = isset( $_POST['subject'] ) ? sanitize_text_field( wp_unslash( $_POST['subject'] ) ) : '';
        $message = isset( $_POST['message'] ) ? wp_kses_post( wp_unslash( $_POST['message'] ) ) : '';
        $category = isset( $_POST['category'] ) ? sanitize_key( wp_unslash( $_POST['category'] ) ) : 'other';
        $priority = isset( $_POST['priority'] ) ? sanitize_key( wp_unslash( $_POST['priority'] ) ) : 'normal';
        if ( $subject === '' || $message === '' ) {
            wp_send_json_error( array( 'error' => esc_html__( 'Sujet et message requis.', 'falcify-connector' ) ), 400 );
        }
        $payload = array(
            'api_key'     => $api_key,
            'site_url'    => home_url(),
            'subject'     => $subject,
            'message'     => $message,
            'category'    => $category,
            'priority'    => $priority,
            'diagnostics' => self::collect_diagnostics(),
        );
        $r = Falcify_Api::request( '/api/wordpress/support', $payload );
        if ( ! $r['ok'] ) {
            wp_send_json_error( $r['data'] ?: array( 'error' => $r['error'] ?? esc_html__( 'Erreur', 'falcify-connector' ) ), $r['status'] ?? 400 );
        }
        wp_send_json_success( $r['data'] );
    }
}
