<?php
/**
 * TamRank Multiple Keywords Manager
 *
 * Beheert primary en secondary keywords voor posts
 *
 * @package TamRank
 * @since 6.5.0
 */

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

class TamRank_Keywords {

    /**
     * Get all keywords for a post
     *
     * @param int $post_id Post ID
     * @return array Array met 'primary' en 'secondary' keywords
     */
    public static function get_keywords($post_id) {
        $primary = get_post_meta($post_id, '_tam_rank_focus_keyword', true);
        $secondary = get_post_meta($post_id, '_tamrank_secondary_keywords', true);

        // Ensure secondary is array
        if (!is_array($secondary)) {
            $secondary = !empty($secondary) ? array_filter(array_map('trim', explode(',', $secondary))) : [];
        }

        return [
            'primary' => $primary ?: '',
            'secondary' => array_values(array_filter($secondary)) // Remove empty values
        ];
    }

    /**
     * Save keywords for a post
     *
     * @param int $post_id Post ID
     * @param string $primary Primary keyword
     * @param array $secondary Array van secondary keywords (max 4)
     */
    public static function save_keywords($post_id, $primary, $secondary = []) {
        // Save primary keyword
        update_post_meta($post_id, '_tam_rank_focus_keyword', sanitize_text_field($primary));

        // Save secondary keywords (max 4)
        if (is_array($secondary)) {
            $secondary = array_slice($secondary, 0, 4); // Max 4 secondary
            $secondary = array_map('sanitize_text_field', $secondary);
            $secondary = array_filter($secondary); // Remove empty
            update_post_meta($post_id, '_tamrank_secondary_keywords', $secondary);
        }
    }





    /**
     * Get keyword suggestions based on primary
     * (Simpele implementatie - kan later uitgebreid worden met API)
     *
     * @param string $primary_keyword Primary keyword
     * @return array Suggesties voor secondary keywords
     */
    public static function get_keyword_suggestions($primary_keyword) {
        $suggestions = [];

        if (empty($primary_keyword)) {
            return $suggestions;
        }

        // Basis suggesties: variaties van primary keyword
        $words = explode(' ', $primary_keyword);

        // Voeg "best" toe
        if (!stripos($primary_keyword, 'best')) {
            $suggestions[] = 'best ' . $primary_keyword;
        }

        // Voeg jaar toe
        $current_year = gmdate('Y');
        if (!stripos($primary_keyword, $current_year)) {
            $suggestions[] = $primary_keyword . ' ' . $current_year;
        }

        // Voeg "how to" toe voor non-question keywords
        if (!stripos($primary_keyword, 'how') && !stripos($primary_keyword, 'what')) {
            $suggestions[] = 'how to ' . $primary_keyword;
        }

        // Voeg "tips" toe
        if (!stripos($primary_keyword, 'tips')) {
            $suggestions[] = $primary_keyword . ' tips';
        }

        return array_slice($suggestions, 0, 4);
    }
}
