<?php
/**
 * API handler for GistMag Alt Text Generator
 *
 * @package GistMag_ATG
 */

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

class GistMag_ATG_API {
    private $api_base_url;

    public function __construct() {
        $this->api_base_url = get_option('gistmag_atg_api_base_url', GISTMAG_ATG_API_BASE_URL);
    }

    public function generate_alt_text($image_url, $mode = 'short', $api_key = '') {
        if (empty($api_key)) {
            $api_key = get_option('gistmag_atg_api_key');
        }

        if (empty($api_key)) {
            return false;
        }

        // Use the public endpoint
        $url = $this->api_base_url . '/generate-public';
        
        $response = wp_remote_post($url, array(
            'headers' => array(
                'Content-Type' => 'application/json',
            ),
            'body' => json_encode(array(
                'image_url' => $image_url,
                'mode' => $mode,
                'api_key' => $api_key,
                'source' => 'wordpress',
            )),
            'timeout' => 30,
        ));

        if (is_wp_error($response)) {
            return false;
        }

        $status_code = wp_remote_retrieve_response_code($response);
        $body = wp_remote_retrieve_body($response);
        $data = json_decode($body, true);

        if ($status_code !== 200) {
            return false;
        }

        if (isset($data['alt_text'])) {
            return sanitize_text_field($data['alt_text']);
        }

        return false;
    }

    public function verify_api_key($api_key) {
        if (empty($api_key)) {
            return false;
        }

        // Use the public verify endpoint
        $url = $this->api_base_url . '/verify-key-public';
        
        $response = wp_remote_post($url, array(
            'headers' => array(
                'Content-Type' => 'application/json',
            ),
            'body' => json_encode(array(
                'api_key' => $api_key,
            )),
            'timeout' => 10,
        ));

        if (is_wp_error($response)) {
            return false;
        }

        $status_code = wp_remote_retrieve_response_code($response);
        $body = wp_remote_retrieve_body($response);
        $data = json_decode($body, true);

        return $status_code === 200 && isset($data['valid']) && $data['valid'] === true;
    }

    public function get_usage_stats($api_key) {
        if (empty($api_key)) {
            return false;
        }

        // Use the public usage endpoint
        $url = $this->api_base_url . '/usage-public';
        
        $response = wp_remote_post($url, array(
            'headers' => array(
                'Content-Type' => 'application/json',
            ),
            'body' => json_encode(array(
                'api_key' => $api_key,
            )),
            'timeout' => 10,
        ));

        if (is_wp_error($response)) {
            return false;
        }

        $status_code = wp_remote_retrieve_response_code($response);
        $body = wp_remote_retrieve_body($response);
        $data = json_decode($body, true);

        if ($status_code !== 200) {
            return false;
        }

        return $data;
    }
}

