<?php

namespace FluentC\Services;

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

use FluentC\Utils\Regex_Helper;
use FluentC\Utils\Placeholder_Manager;
use FluentC\Models\Htmltags;
use PHPHtmlParser\Dom;
use PHPHtmlParser\Dom\Node\HtmlNode;
use PHPHtmlParser\Dom\Node\TextNode;
use PHPHtmlParser\Options;

class Json_Processor {

    public function __construct(){
      
    }

    public function should_process_json($json){
        $data = json_decode($json, true);

        if (!is_array($data)) {
            return true;
        }
        if ($this->checkRequiredElements($data)) {
            return true;
        } else {
            return false;
        }
    }

public function process_json_data($json){


    return $json;

}

function checkRequiredElements($data) {
    // Define all possible paths to check
    $paths = [
        // Top level elements
        ['title'],
        ['today_title'],
        ['prev_label'],
        ['next_label'],
        ['messages'],
        ['notice'],
        ['text'],
        ['url'],
        ['today_url'],
        
        // Subscribe links paths
        ['subscribe_links', 'gcal', 'label'],
        ['subscribe_links', 'ical', 'label'],
        ['subscribe_links', 'outlook-365', 'label'],
        ['subscribe_links', 'ics', 'label'],
        ['subscribe_links', 'outlook-live', 'label'],
        ['subscribe_links', 'outlook-ics', 'label'],
        ['subscribe_links', 'gcal', 'single_label'],
        ['subscribe_links', 'ical', 'single_label'],
        ['subscribe_links', 'outlook-365', 'single_label'],
        ['subscribe_links', 'ics', 'single_label'],
        ['subscribe_links', 'outlook-live', 'single_label'],
        ['subscribe_links', 'outlook-ics', 'single_label'],
        
        // Public views paths
        ['public_views', 'list', 'view_label'],
        ['public_views', 'month', 'view_label'],
        ['public_views', 'day', 'view_label'],
        ['public_views', 'list', 'view_url'],
        ['public_views', 'month', 'view_url'],
        ['public_views', 'day', 'view_url'],
        
        // iCal display link paths
        ['ical', 'display_link', 'link', 'text'],
        ['ical', 'display_link', 'link', 'title']
    ];
    
    // Check if any of the paths exist in the data
    foreach ($paths as $path) {
        $current = $data;
        $valid = true;
        
        // Navigate through each level of the path
        foreach ($path as $key) {
            if (!is_array($current) || !array_key_exists($key, $current)) {
                $valid = false;
                break;
            }
            $current = $current[$key];
        }
        
        // If we found a valid path, return true
        if ($valid) {
            return true;
        }
    }
    
    // If no paths were found, return false
    return false;
}
 }