<?php
/**
 * Plugin Name:       Spawnster: AI Blog Writer and Instant Site Generator
 * Plugin URI:        https://ristrettoapps.com/product/spawnster
 * Description:       Create instant WordPress websites in seconds with AI, complete with multiple pages, filled in text, and matching images, based on any niche.
 * Version:           1.1.6
 * Author:            Ristretto Apps
 * Author URI:        https://ristrettoapps.com/
 * License:           GPL-2.0+
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain:       spawnster
 * Domain Path:       /languages
 */

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
    require_once __DIR__ . '/vendor/autoload.php';
}

/**
 * Currently plugin version.
 * Start at version 1.0.0 and use SemVer - https://semver.org
 * Rename this for your plugin and update it as you release new versions.
 */
define( 'SPAWNSTER_VERSION', '1.1.5' );

/**
 * Plugin name.
 */
define( 'SPAWNSTER_PLUGIN_NAME', 'spawnster' );

/**
 * The code that runs during plugin activation.
 * This action is documented in includes/class-spawnster-activator.php
 */
function spawnster_activate() {
	require_once plugin_dir_path( __FILE__ ) . 'includes/class-spawnster-activator.php';
	Spawnster_Activator::activate();
}

/**
 * The code that runs during plugin deactivation.
 * This action is documented in includes/class-spawnster-deactivator.php
 */
function spawnster_deactivate() {
	require_once plugin_dir_path( __FILE__ ) . 'includes/class-spawnster-deactivator.php';
	Spawnster_Deactivator::deactivate();
}

register_activation_hook( __FILE__, 'spawnster_activate');
register_deactivation_hook( __FILE__, 'spawnster_deactivate');

/**
 * The core plugin class that is used to define internationalization,
 * admin-specific hooks, and public-facing site hooks.
 */
require plugin_dir_path( __FILE__ ) . 'includes/class-spawnster.php';

/**
 * Begins execution of the plugin.
 *
 * Since everything within the plugin is registered via hooks,
 * then kicking off the plugin from this point in the file does
 * not affect the page life cycle.
 *
 * @since    1.0.0
 */
function spawnster_run() {

	$plugin = new Spawnster();
	$plugin->run();

    add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), 'spawnster_add_settings_link');

	// Register custom post type for content generation jobs
	// This will be handled in the admin class to ensure proper timing
	
	// Register cron hook for content generation
	add_action( 'spawnster_generate_content', 'spawnster_generate_content_callback' );
	
	// Add custom cron intervals
	add_filter( 'cron_schedules', 'spawnster_add_cron_intervals' );

}
spawnster_run();

function spawnster_add_settings_link($links): array
{
    $settings_link = '<a href="' . \RistrettoApps\Spawnster\Base\Constants::SPAWNSTER_ADMIN_SETTINGS . '">Settings</a>';
    $links[] = $settings_link;
    return $links;
}



/**
 * Callback function for content generation cron job.
 *
 * @param int $job_id The job ID.
 */
function spawnster_generate_content_callback( int $job_id ): void {
	// Initialize services
	$openai_service = new \RistrettoApps\Spawnster\Services\OpenAiService();
	$google_search_service = new \RistrettoApps\Spawnster\Services\GoogleSearchService();
	$content_research_service = new \RistrettoApps\Spawnster\Services\ContentResearchService( $google_search_service, $openai_service );
	$post_vector_table_service = new \RistrettoApps\Spawnster\Services\PostVectorTableService();
	$scheduled_content_service = new \RistrettoApps\Spawnster\Services\ScheduledContentService( $openai_service, $content_research_service, $post_vector_table_service );
	
	// Generate content
	$scheduled_content_service->generate_ai_content( $job_id );
}

/**
 * Add custom cron intervals.
 *
 * @param array $schedules The existing schedules.
 * @return array The modified schedules.
 */
function spawnster_add_cron_intervals( array $schedules ): array {
	$schedules['spawnster_monthly'] = array(
		'interval' => 30 * DAY_IN_SECONDS,
		'display'  => 'Monthly',
	);
	$schedules['spawnster_yearly'] = array(
		'interval' => YEAR_IN_SECONDS,
		'display'  => 'Yearly',
	);

	return $schedules;
}
