<?php

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

/**
 * Get user billing information
 * 
 * @return WP_REST_Response Response with billing data
 */
function content_craft_ai_get_billing()
{
    // Get API key
    $api_key = get_option('content_craft_ai_api_key');
    if (empty($api_key)) {
        return new WP_REST_Response(
            array(
                'success' => false,
                'message' => 'Authentication required',
            ),
            401
        );
    }

    // Make request to external API
    $response = wp_remote_get(
        CONTENT_CRAFT_AI_EXTERNAL_API . '/user/billing',
        array(
            'headers' => array(
                'x-api-key' => $api_key,
                'x-wp-domain' => wp_parse_url(get_site_url(), PHP_URL_HOST),
                'Content-Type' => 'application/json',
            ),
            'timeout' => 120,
        )
    );

    // Check for error
    if (is_wp_error($response)) {
        return new WP_REST_Response(
            array(
                'success' => false,
                'message' => $response->get_error_message(),
            ),
            500
        );
    }

    $body = wp_remote_retrieve_body($response);
    $status = wp_remote_retrieve_response_code($response);

    // Return the response
    return new WP_REST_Response(
        json_decode($body, true),
        $status
    );
}

/**
 * Update user billing information
 * 
 * @param WP_REST_Request $request The request object
 * @return WP_REST_Response Response with updated billing data
 */
function content_craft_ai_update_billing($request)
{
    // Get API key
    $api_key = get_option('content_craft_ai_api_key');
    if (empty($api_key)) {
        return new WP_REST_Response(
            array(
                'success' => false,
                'message' => 'Authentication required',
            ),
            401
        );
    }

    // Get billing data from request
    $billing_data = array(
        'street' => $request->get_param('street'),
        'city' => $request->get_param('city'),
        'state' => $request->get_param('state'),
        'country' => $request->get_param('country'),
        'zipcode' => $request->get_param('zipcode'),
    );

    // Validate required fields
    $required_fields = ['street', 'city', 'country', 'zipcode'];
    foreach ($required_fields as $field) {
        if (empty($billing_data[$field])) {
            return new WP_REST_Response(
                array(
                    'success' => false,
                    'message' => ucfirst($field) . ' is required',
                ),
                400
            );
        }
    }

    // Make request to external API
    $response = wp_remote_post(
        CONTENT_CRAFT_AI_EXTERNAL_API . '/user/billing',
        array(
            'headers' => array(
                'x-api-key' => $api_key,
                'x-wp-domain' => wp_parse_url(get_site_url(), PHP_URL_HOST),
                'Content-Type' => 'application/json',
            ),
            'body' => json_encode($billing_data),
            'timeout' => 120,
        )
    );

    // Check for error
    if (is_wp_error($response)) {
        return new WP_REST_Response(
            array(
                'success' => false,
                'message' => $response->get_error_message(),
            ),
            500
        );
    }

    $body = wp_remote_retrieve_body($response);
    $status = wp_remote_retrieve_response_code($response);

    // Return the response
    return new WP_REST_Response(
        json_decode($body, true),
        $status
    );
}
