<?php

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

/**
 * Article Status Controller
 * 
 * Handles operations related to article generation status
 */
class ContentCraftAI_ArticleStatusController
{
  /**
   * Database manager instance
   * @var ContentCraftAI_ArticleStatusManager
   */
  private $db_manager;

  /**
   * Constructor
   */
  public function __construct()
  {
    // Get database manager instances
    $this->db_manager = ContentCraftAI_ArticleStatusManager::get_instance();
  }

  /**
   * Get paginated generations with filters
   * 
   * @param int   $page      Current page number
   * @param int   $per_page  Number of items per page
   * @param array $filters   Array of filters to apply
   * 
   * @return array|bool Filtered generations data or false on failure
   */
  public function get_paginated_generations($page, $per_page, $filters = [])
  {
    return $this->db_manager->get_paginated_generations($page, $per_page, $filters);
  }

  /**
   * Get a specific generation by queue ID
   * 
   * @param string $queue_id The queue ID to look up
   * 
   * @return array|bool Generation data or false on failure
   */
  public function get_generation_by_queue_id($queue_id)
  {
    // Get the generation manager instance
    $generation_manager = ContentCraftAI_ArticleGenerationManager::get_instance();

    // Get the generation from the database
    $generation = $generation_manager->get_generation_by_queue_id($queue_id);

    if (!$generation) {
      return false;
    }

    // Add thumbnail URL if post has a featured image
    $thumbnail_url = '';
    if (!empty($generation->post_id)) {
      $thumbnail_id = get_post_thumbnail_id($generation->post_id);
      if ($thumbnail_id) {
        $thumbnail = wp_get_attachment_image_src($thumbnail_id, 'thumbnail');
        if ($thumbnail) {
          $thumbnail_url = $thumbnail[0];
        }
      }
    }

    // Decode parameters
    $parameters = !empty($generation->parameters) ? json_decode($generation->parameters, true) : [];

    // Format the response
    return [
      'id' => $generation->id,
      'queue_id' => $generation->queue_id,
      'title' => $generation->title,
      'status' => $generation->status,
      'word_count' => $generation->word_count,
      'created_at' => $generation->created_at,
      'completed_at' => $generation->completed_at,
      'scheduled_time' => $generation->scheduled_time,
      'is_scheduled' => (bool) $generation->is_scheduled,
      'post_id' => $generation->post_id,
      'thumbnail_url' => $thumbnail_url,
      'parameters' => $parameters
    ];
  }

  /**
   * Update the status of a generation
   * 
   * @param string $queue_id The queue ID to update
   * @param string $status   The new status value
   * 
   * @return bool True on success, false on failure
   */
  public function update_generation_status($queue_id, $status)
  {
    // Get the generation manager instance
    $generation_manager = ContentCraftAI_ArticleGenerationManager::get_instance();

    // Update the status
    return $generation_manager->update_generation_status($queue_id, $status);
  }

  /**
   * Update the parameters of a scheduled article
   * 
   * @param string $queue_id   The queue ID to update
   * @param array  $parameters The new parameters
   * 
   * @return bool True on success, false on failure
   */
  public function update_generation_parameters($queue_id, $parameters)
  {
    return $this->db_manager->update_generation_parameters($queue_id, $parameters);
  }
}
