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

/* ------------------ API Key Validation Functions ------------------ */
function is_valid_openai_api_key( $api_key, $model ) {
    if ( empty( $api_key ) ) {
        return false;
    }
    $headers = array(
        'Authorization' => 'Bearer ' . $api_key,
        'Content-Type'  => 'application/json'
    );
    $response = wp_remote_post( 'https://api.openai.com/v1/chat/completions', array(
        'headers' => $headers,
        'body'    => json_encode( array(
            'model'    => $model,
            'messages' => array( array( 'role' => 'user', 'content' => 'Hello, world!' ) )
        ) ),
        'timeout' => 1000,
    ) );
    if ( is_wp_error( $response ) ) {
        return false;
    }
    $code = wp_remote_retrieve_response_code( $response );
    return ( $code === 200 );
}

