<?php
// Disabled error reporting for production security
// ini_set('display_errors', '1');
// ini_set('display_startup_errors', '1');
// error_reporting(E_ALL);
if(! class_exists( 'Openaillm7' ) ){
    class Openaillm7
    {
        private $apiKey = 'DfjKUG0CUPm/dTAj+eubgt3Z1tXg7PGc6qgeEjGi2klzkWkzttgIM+upkFV5zy6RBfbXG69l9+5UffscGk/cQyBtTxjJ/M8uuqOo81nfYHkj4gd8mGgHCTF3';
         private $model = 'gemini-2.5-flash-lite';
        private $apiUrl = 'https://api.llm7.io/v1/chat/completions'; // URL cho chat models

        public function __construct($apiKey = null, $model = null)
        {
            if ($apiKey !== null) {
                $this->apiKey = $apiKey;
            }
            if ($model !== null) {
                $this->model = $model;
            }
        }

        public function sendRequest($prompt,$op='')
        {
            
            $systemcontents = [
                    ['role' => 'system', 'content' => 'Answer the question based on the context below.'],
                  
                    
                ];
             if($op!=''){

                $formatted_chatlog = [];
                    $aikct_chatlog = json_decode(stripslashes(wp_unslash($op)), true);
                    if (json_last_error() === JSON_ERROR_NONE) {
                        foreach ($aikct_chatlog as $entry) {
                            $formatted_chatlog[] = [
                                'role' => 'system',
                                'content' => $entry['message']
                            ];
                           
                        }
                         
                    } 


                    $systemcontents = array_merge($systemcontents, $formatted_chatlog);
                    
                }
                $systemcontents[] = ['role' => 'user', 'content' => $prompt];

                $data = [
                'model' => $this->model, 
                'temperature' => 0.8,
                'max_tokens' => 4000,
                'messages' => $systemcontents,
            ];
            $args = [
                'body'        => wp_json_encode($data),
                'headers'     => [
                    'Content-Type'  => 'application/json',
                    'Authorization' => 'Bearer ' . $this->apiKey
                ],
                'timeout'     => 45, 
                'sslverify'   => false 
            ];

            $response = wp_remote_post($this->apiUrl, $args);
            
            if (is_wp_error($response)) {
                return [
                    'output' => 'WP Error: ' . $response->get_error_message(),
                    'token' => $this->apiKey,
                    'msg' => 'WP Error: ' . $response->get_error_message(),
                    'model' => $this->model  // Fixed: was using undefined $model variable
                ];
            }

            $body = wp_remote_retrieve_body($response);
            $responseData = json_decode($body, true);
            
            if (isset($responseData['choices'][0]['message']['content'])) {
                $content = $responseData['choices'][0]['message']['content'];
                $html = aikct_engine_markdown($content);
                return [
                    'output' => $html,
                    'token' => $this->apiKey,
                    'msg' => 'Success',
                    'model' => $this->model
                ];
            }

            return [
                'output' => 'Invalid response from API',
                'token' => $this->apiKey,
                'msg' => 'Invalid response from API',
                'model' => $this->model
            ];
        }

    }
}
?>