<?php
if (!defined('ABSPATH')) { exit; }
add_action('after_setup_theme', function () {
    add_theme_support('title-tag');
    add_theme_support('post-thumbnails');
    add_theme_support('html5', array('search-form','gallery','caption','style','script'));
});
add_action('wp_enqueue_scripts', function () {
    wp_enqueue_style('netiks-business', get_stylesheet_uri(), array(), '1.0.0');
    wp_enqueue_script('netiks-business', get_theme_file_uri('site.js'), array(), '1.0.0', true);
});
function netiks_materialize($html) {
    $html = str_replace('@@ASSET@@', esc_url(get_theme_file_uri('assets')), $html);
    $html = preg_replace_callback('~href="(/[^"\\s]*)"~', function ($m) {
        return 'href="' . esc_url(home_url($m[1])) . '"';
    }, $html);
    return $html;
}
add_action('admin_menu', function () {
    add_theme_page('Netiks starter pages','Netiks starter pages','manage_options','netiks-starter','netiks_starter_screen');
});
function netiks_starter_screen() {
    if (!current_user_can('manage_options')) { return; }
    echo '<div class="wrap"><h1>Netiks starter pages</h1><p>Create the five designed pages as drafts. Existing pages are never overwritten. Review and publish them, then select the new Home page under Settings → Reading.</p>';
    if (isset($_POST['netiks_create'])) {
        check_admin_referer('netiks_starter');
        $pages = json_decode(file_get_contents(get_theme_file_path('content.json')), true);
        foreach ($pages as $slug => $page) {
            $existing = get_posts(array('post_type'=>'page','post_status'=>'any','meta_key'=>'_netiks_starter_slug','meta_value'=>$slug,'numberposts'=>1));
            if ($existing) { echo '<p>Already created: ' . esc_html($slug) . '</p>'; continue; }
            $body = netiks_materialize($page['body']);
            $id = wp_insert_post(array('post_type'=>'page','post_status'=>'draft','post_title'=>$slug === 'home' ? 'Home' : ($slug === 'ip-track' ? 'IP-Track' : ucfirst($slug)), 'post_name'=>$slug, 'post_content'=>'<!-- wp:html -->' . $body . '<!-- /wp:html -->','meta_input'=>array('_netiks_starter_slug'=>$slug,'_netiks_description'=>$page['description'])), true);
            if (is_wp_error($id)) { echo '<p>' . esc_html($id->get_error_message()) . '</p>'; }
            else { echo '<p>Draft created: <a href="' . esc_url(get_edit_post_link($id)) . '">' . esc_html($slug) . '</a></p>'; }
        }
    }
    echo '<form method="post">'; wp_nonce_field('netiks_starter');
    echo '<p><button type="submit" class="button button-primary" name="netiks_create" value="1">Create starter drafts</button></p></form><p>The pages use styled HTML in the WordPress editor. This theme does not require Pagelayer. Enquiries prepare an email draft; they do not send or store submissions.</p></div>';
}
add_action('wp_head', function () {
    if (is_singular()) {
        $description = get_post_meta(get_queried_object_id(), '_netiks_description', true);
        if ($description) { echo '<meta name="description" content="' . esc_attr($description) . '">' . "\n"; }
    }
});

/* Use the configured WPForms contact form on the designed Contact page. */
add_filter('the_content', function ($content) {
    if (is_page('contact') && function_exists('do_shortcode')) {
        $replacement = '<div class="netiks-wpforms-contact"><h3>Send an enquiry</h3><p class="form-note">Tell us about your project or request an IP-Track demonstration.</p>' . do_shortcode('[wpforms id="138" title="false" description="false"]') . '</div>';
        $content = preg_replace('~<form id="enquiry">.*?</form>~s', $replacement, $content, 1);
    }
    return $content;
}, 20);

