<?php
namespace Atlasly\Core;

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

class Network {
    
    public static function init() {
        if(!is_multisite()) return;
        
        add_action('network_admin_menu', [self::class, 'network_menu']);
        add_action('wp_ajax_atlasly_sync_content', [self::class, 'sync_content']);
    }
    
    public static function network_menu() {
        add_menu_page(
            'Atlasly Network',
            'Atlasly Network', 
            'manage_network',
            'atlasly-network',
            [self::class, 'network_page'],
            'dashicons-database'
        );
    }
    
    public static function network_page() {
        $sites = get_sites(['number' => 100]);
        echo '<div class="wrap">';
        echo '<h1>Atlasly Network</h1>';
        echo '<div id="atlasly-network-app" data-sites="' . esc_attr(wp_json_encode($sites)) . '"></div>';
        echo '</div>';
    }
    
    public static function sync_content() {
        check_ajax_referer('atlasly_network_sync', 'nonce');

        if(!current_user_can('manage_network')) {
            wp_send_json_error(['message' => 'Unauthorized'], 403);
            return;
        }

        $source_site = isset($_POST['source_site']) ? absint(wp_unslash($_POST['source_site'])) : 0;
        $target_sites_raw = isset($_POST['target_sites']) ? (array) wp_unslash($_POST['target_sites']) : [];
        $content_types_raw = isset($_POST['content_types']) ? (array) wp_unslash($_POST['content_types']) : [];
        $target_sites = array_filter(array_unique(array_map('absint', $target_sites_raw)));
        $content_types = array_filter(array_unique(array_map('absint', $content_types_raw)));

        if(!$source_site || empty($target_sites) || empty($content_types)) {
            wp_send_json_error(['message' => 'Invalid request payload'], 400);
            return;
        }
        
        switch_to_blog($source_site);
        global $wpdb;
        
        $types_table = $wpdb->prefix . 'atlasly_content_types';
        $entries_table = $wpdb->prefix . 'atlasly_entries';
        
        $types = $wpdb->get_results($wpdb->prepare(
            "SELECT * FROM $types_table WHERE id IN (" . implode(',', array_fill(0, count($content_types), '%d')) . ")",
            ...$content_types
        ), ARRAY_A);
        
        $entries = $wpdb->get_results($wpdb->prepare(
            "SELECT * FROM $entries_table WHERE content_type_id IN (" . implode(',', array_fill(0, count($content_types), '%d')) . ")",
            ...$content_types
        ), ARRAY_A);
        
        restore_current_blog();
        
        foreach($target_sites as $site_id) {
            switch_to_blog($site_id);
            
            foreach($types as $type) {
                $wpdb->replace($wpdb->prefix . 'atlasly_content_types', [
                    'id' => $type['id'],
                    'name' => $type['name'],
                    'slug' => $type['slug'],
                    'settings' => $type['settings'],
                    'status' => $type['status']
                ]);
            }
            
            foreach($entries as $entry) {
                $wpdb->replace($wpdb->prefix . 'atlasly_entries', [
                    'id' => $entry['id'],
                    'content_type_id' => $entry['content_type_id'],
                    'title' => $entry['title'],
                    'slug' => $entry['slug'],
                    'data' => $entry['data'],
                    'status' => $entry['status'],
                    'author_id' => $entry['author_id']
                ]);
            }
            
            restore_current_blog();
        }
        
        wp_send_json_success(['synced' => count($target_sites) * (count($types) + count($entries))]);
    }
    
    public static function get_network_stats() {
        if(!is_multisite()) return [];
        
        $sites = get_sites(['number' => 100]);
        $stats = [];
        
        foreach($sites as $site) {
            switch_to_blog($site->blog_id);
            global $wpdb;
            
            $types_count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}atlasly_content_types");
            $entries_count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}atlasly_entries");
            
            $stats[] = [
                'site_id' => $site->blog_id,
                'site_name' => get_bloginfo('name'),
                'content_types' => (int)$types_count,
                'entries' => (int)$entries_count
            ];
            
            restore_current_blog();
        }
        
        return $stats;
    }
}
