<?php

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

/**
 * This file depends on WordPress core functionality.
 * The following functions are defined by WordPress and will be available at runtime:
 * - current_time: Function to get the current WordPress time
 * - wp_remote_post: Function to make HTTP POST requests
 * - wp_remote_get: Function to make HTTP GET requests
 * - is_wp_error: Function to check if a value is a WP_Error
 * - wp_remote_retrieve_body: Function to get body content from an HTTP response
 * - get_option: Function to retrieve an option from the database
 * - site_url: Function to get the site URL
 * - get_edit_post_link: Function to get the edit link for a post
 * - get_current_user_id: Function to get the current WordPress user ID
 */

/**
 * Base controller class for article generation
 * Provides common functionality for all article generator controllers
 */
class ContentCraftAI_BaseController
{
    /**
     * Get the API URL for making requests
     * 
     * @return string API URL
     */
    protected function get_api_url()
    {
        return defined('CONTENT_CRAFT_AI_EXTERNAL_API')
            ? CONTENT_CRAFT_AI_EXTERNAL_API
            : '';
    }

    /**
     * Get common API request arguments like headers
     * 
     * @param array $body Optional request body
     * @return array Request arguments for wp_remote functions
     */
    protected function get_api_request_args($body = null)
    {
        // Get API key
        $api_key = get_option('content_craft_ai_api_key', '');

        // Prepare headers
        $headers = [
            'Content-Type' => 'application/json',
            'x-api-key' => $api_key,
        ];

        // Prepare request args
        $request_args = [
            'headers' => $headers,
            'timeout' => 120
        ];

        // Add body if provided
        if ($body !== null) {
            $request_args['body'] = json_encode($body, JSON_UNESCAPED_UNICODE);
        }

        // Add domain header if function exists
        if (function_exists('ContentCraft::content_craft_addDomainHeader')) {
            $request_args = ContentCraft::content_craft_addDomainHeader($request_args);
        } else {
            // Fallback method to add domain header
            $domain = isset($_SERVER['HTTP_HOST']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_HOST'])) : site_url();
            $request_args['headers']['x-wp-domain'] = $domain;
        }

        return $request_args;
    }

    /**
     * Format a timestamp using WordPress date/time formatting
     * 
     * @param int|null $timestamp Optional timestamp, defaults to current time
     * @return string Formatted date/time string
     */
    protected function format_datetime($timestamp = null)
    {
        if ($timestamp === null) {
            $timestamp = time();
        }

        if (function_exists('current_time')) {
            return current_time('mysql', false);
        }

        return gmdate('Y-m-d H:i:s', $timestamp);
    }
}
