<?php
/**
 * PHPUnit Bootstrap
 *
 * Sets up the minimal WordPress environment for testing.
 *
 * @package WP_Agentic_Writer
 */

// Define plugin constants if not already defined.
if (!defined('WPAW_PLUGIN_DIR')) {
    define('WPAW_PLUGIN_DIR', dirname(__DIR__) . '/');
}

if (!defined('WP_AGENTIC_WRITER_URL')) {
    define('WP_AGENTIC_WRITER_URL', 'http://localhost/');
}

// WordPress stub functions.
if (!function_exists('wp_json_encode')) {
    function wp_json_encode($data, $options = 0, $depth = 512) {
        return json_encode($data, $options, $depth);
    }
}

if (!function_exists('wp_parse_args')) {
    function wp_parse_args($args, $defaults = []) {
        if (is_array($args)) {
            return array_merge($defaults, $args);
        }
        parse_str($args, $r);
        return array_merge($defaults, $r);
    }
}

if (!function_exists('sanitize_text_field')) {
    function sanitize_text_field($str) {
        return trim(strip_tags($str));
    }
}

if (!function_exists('sanitize_textarea_field')) {
    function sanitize_textarea_field($str) {
        return sanitize_text_field($str);
    }
}

if (!function_exists('untrailingslashit')) {
    function untrailingslashit($str) {
        return rtrim($str, '/\\');
    }
}

if (!function_exists('trailingslashit')) {
    function trailingslashit($str) {
        return rtrim($str, '/\\') . '/';
    }
}

if (!function_exists('get_option')) {
    function get_option($key, $default = false) {
        static $options = [];
        return $options[$key] ?? $default;
    }
}

if (!function_exists('update_option')) {
    function update_option($key, $value) {
        static $options = [];
        $options[$key] = $value;
        return true;
    }
}

if (!function_exists('get_post_meta')) {
    function get_post_meta($post_id, $key = '', $single = false) {
        static $meta = [];
        if ($key === '') {
            return $meta[$post_id] ?? [];
        }
        if ($single) {
            return $meta[$post_id][$key][0] ?? '';
        }
        return $meta[$post_id][$key] ?? [];
    }
}

if (!function_exists('get_transient')) {
    function get_transient($key) {
        static $transients = [];
        return $transients[$key] ?? false;
    }
}

if (!function_exists('set_transient')) {
    function set_transient($key, $value, $expiration = 0) {
        static $transients = [];
        $transients[$key] = $value;
        return true;
    }
}

if (!function_exists('delete_transient')) {
    function delete_transient($key) {
        static $transients = [];
        unset($transients[$key]);
        return true;
    }
}

if (!function_exists('wp_remote_post')) {
    function wp_remote_post($url, $args = []) {
        return new WP_Error('http_request_failed', 'cURL error 7: Failed to connect to host');
    }
}

if (!function_exists('wp_remote_get')) {
    function wp_remote_get($url, $args = []) {
        return new WP_Error('http_request_failed', 'cURL error 7: Failed to connect to host');
    }
}

if (!function_exists('wp_remote_retrieve_response_code')) {
    function wp_remote_retrieve_response_code($response) {
        if (is_wp_error($response)) {
            return 0;
        }
        return $response['response']['code'] ?? 200;
    }
}

if (!function_exists('wp_remote_retrieve_body')) {
    function wp_remote_retrieve_body($response) {
        if (is_wp_error($response)) {
            return '';
        }
        return $response['body'] ?? '';
    }
}

if (!function_exists('wp_strip_all_tags')) {
    function wp_strip_all_tags($string) {
        return strip_tags($string);
    }
}

if (!function_exists('__')) {
    function __($text, $domain = 'default') {
        return $text;
    }
}

if (!function_exists('esc_html__')) {
    function esc_html__($text, $domain = 'default') {
        return esc_html($text);
    }
}

if (!function_exists('esc_attr__')) {
    function esc_attr__($text, $domain = 'default') {
        return esc_attr($text);
    }
}

if (!function_exists('esc_html')) {
    function esc_html($text) {
        return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
    }
}

if (!function_exists('esc_attr')) {
    function esc_attr($text) {
        return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
    }
}

if (!function_exists('esc_url')) {
    function esc_url($url) {
        return filter_var($url, FILTER_SANITIZE_URL);
    }
}

if (!function_exists('is_wp_error')) {
    function is_wp_error($thing) {
        return $thing instanceof WP_Error;
    }
}

if (!function_exists('wp_send_json_success')) {
    function wp_send_json_success($data = null, $status = null) {
        echo json_encode(['success' => true, 'data' => $data]);
        if ($status) {
            http_response_code($status);
        }
        exit;
    }
}

if (!function_exists('wp_send_json_error')) {
    function wp_send_json_error($data = null, $status = null) {
        echo json_encode(['success' => false, 'data' => $data]);
        if ($status) {
            http_response_code($status);
        }
        exit;
    }
}

if (!function_exists('current_time')) {
    function current_time($type) {
        return time();
    }
}

if (!function_exists('wp_cache_get')) {
    function wp_cache_get($key, $group = '') {
        static $cache = [];
        return $cache[$group][$key] ?? false;
    }
}

if (!function_exists('wp_cache_set')) {
    function wp_cache_set($key, $data, $group = '', $expire = 0) {
        static $cache = [];
        $cache[$group][$key] = $data;
        return true;
    }
}

if (!function_exists('wp_cache_delete')) {
    function wp_cache_delete($key, $group = '') {
        static $cache = [];
        unset($cache[$group][$key]);
        return true;
    }
}

if (!function_exists('did_action')) {
    function did_action($hook) {
        return 0;
    }
}

if (!function_exists('add_action')) {
    function add_action($hook, $callback, $priority = 10, $accepted_args = 1) {
        return true;
    }
}

if (!function_exists('add_filter')) {
    function add_filter($hook, $callback, $priority = 10, $accepted_args = 1) {
        return true;
    }
}

if (!function_exists('apply_filters')) {
    function apply_filters($tag, $value) {
        return $value;
    }
}

if (!function_exists('doing_action')) {
    function doing_action($hook) {
        return false;
    }
}

if (!function_exists('wp_die')) {
    function wp_die($message = '', $title = '', $args = []) {
        echo $message;
        exit(1);
    }
}

if (!function_exists('error_log')) {
    function error_log($message) {
        fwrite(STDERR, $message . PHP_EOL);
    }
}

if (!function_exists('wpautop')) {
    function wpautop($text) {
        return $text;
    }
}

if (!function_exists('get_user_by')) {
    function get_user_by($field, $value) {
        return null;
    }
}

if (!function_exists('get_current_user_id')) {
    function get_current_user_id() {
        return 1;
    }
}

if (!function_exists('current_user_can')) {
    function current_user_can($capability, ...$args) {
        return true;
    }
}

if (!function_exists('is_user_logged_in')) {
    function is_user_logged_in() {
        return true;
    }
}

if (!function_exists('get_post')) {
    function get_post($post) {
        return null;
    }
}

if (!function_exists('get_post_type')) {
    function get_post_type($post = null) {
        return 'post';
    }
}

if (!function_exists('get_the_title')) {
    function get_the_title($post = 0) {
        return 'Test Post';
    }
}

if (!function_exists('get_post_status')) {
    function get_post_status($post = null) {
        return 'publish';
    }
}

if (!function_exists('wp_kses_post')) {
    function wp_kses_post($string) {
        return $string;
    }
}

if (!function_exists('mb_substr')) {
    function mb_substr($str, $start, $length = null) {
        if ($length === null) {
            return substr($str, $start);
        }
        return substr($str, $start, $length);
    }
}

// WP_Error stub class.
class WP_Error {
    public $errors = [];
    public $error_data = [];

    public function __construct($code = '', $message = '', $data = '') {
        if (!empty($code)) {
            $this->errors[$code] = [$message];
            $this->error_data[$code] = $data;
        }
    }

    public function get_error_code() {
        $codes = $this->get_error_codes();
        return $codes[0] ?? '';
    }

    public function get_error_message($code = '') {
        if (empty($code)) {
            $code = $this->get_error_code();
        }
        return $this->errors[$code][0] ?? '';
    }

    public function get_error_codes() {
        return array_keys($this->errors);
    }

    public function get_error_data($code = '') {
        if (empty($code)) {
            $code = $this->get_error_code();
        }
        return $this->error_data[$code] ?? '';
    }

    public function add($code, $message, $data = '') {
        $this->errors[$code][] = $message;
        if (!empty($data)) {
            $this->error_data[$code] = $data;
        }
    }
}

// WP_Post stub class.
class WP_Post {
    public $ID = 0;
    public $post_author = 1;
    public $post_title = '';
    public $post_content = '';
    public $post_status = 'publish';
    public $post_type = 'post';
}

// Load the plugin files we want to test.
require_once WPAW_PLUGIN_DIR . 'includes/class-model-registry.php';
