<?php

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

class HVAC_Profile_Sync_Handler {
    
    private static $instance = null;
    private static $sync_in_progress = [];
    
    public static function get_instance() {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    private function __construct() {
        // Hook into user and profile updates
        add_action('profile_update', [$this, 'sync_user_to_profile'], 10, 2);
        add_action('save_post_trainer_profile', [$this, 'sync_profile_to_user'], 10, 2);
        
        // Schedule sync verification
        add_action('init', [$this, 'schedule_sync_verification']);
        add_action('hvac_verify_sync_integrity', [$this, 'verify_sync_integrity']);
    }
    
    public function sync_user_to_profile($user_id, $old_user_data = null) {
        // Prevent infinite loops
        $sync_key = "user_to_profile_{$user_id}";
        if (isset(self::$sync_in_progress[$sync_key])) {
            return;
        }
        
        self::$sync_in_progress[$sync_key] = true;
        
        try {
            $profile_id = get_user_meta($user_id, 'trainer_profile_id', true);
            if (!$profile_id) {
                unset(self::$sync_in_progress[$sync_key]);
                return;
            }
            
            // Get current user data
            $user = get_userdata($user_id);
            if (!$user) {
                unset(self::$sync_in_progress[$sync_key]);
                return;
            }
            
            // Sync shared fields
            $sync_fields = [
                'first_name' => 'trainer_first_name',
                'last_name' => 'trainer_last_name',
                'display_name' => 'trainer_display_name'
            ];
            
            $needs_update = false;
            foreach ($sync_fields as $user_field => $profile_field) {
                $user_value = $user->$user_field;
                $profile_value = get_post_meta($profile_id, $profile_field, true);
                
                // Only update if values differ
                if ($user_value !== $profile_value) {
                    update_post_meta($profile_id, $profile_field, $user_value);
                    update_post_meta($profile_id, "_{$profile_field}_modified", time());
                    $needs_update = true;
                }
            }
            
            if ($needs_update) {
                error_log("HVAC Profile Sync: User {$user_id} synced to profile {$profile_id}");
            }
            
        } catch (Exception $e) {
            error_log("HVAC Profile Sync Error: " . $e->getMessage());
        } finally {
            unset(self::$sync_in_progress[$sync_key]);
        }
    }
    
    public function sync_profile_to_user($post_id, $post = null) {
        if (get_post_type($post_id) !== 'trainer_profile') {
            return;
        }
        
        // Prevent infinite loops
        $sync_key = "profile_to_user_{$post_id}";
        if (isset(self::$sync_in_progress[$sync_key])) {
            return;
        }
        
        self::$sync_in_progress[$sync_key] = true;
        
        try {
            $user_id = get_post_meta($post_id, 'user_id', true);
            if (!$user_id) {
                unset(self::$sync_in_progress[$sync_key]);
                return;
            }
            
            $user = get_userdata($user_id);
            if (!$user) {
                unset(self::$sync_in_progress[$sync_key]);
                return;
            }
            
            // Sync shared fields
            $sync_fields = [
                'trainer_first_name' => 'first_name',
                'trainer_last_name' => 'last_name', 
                'trainer_display_name' => 'display_name'
            ];
            
            $update_data = ['ID' => $user_id];
            $needs_update = false;
            
            foreach ($sync_fields as $profile_field => $user_field) {
                $profile_value = get_post_meta($post_id, $profile_field, true);
                
                // Get current user value for comparison
                $current_user_value = $user->$user_field;
                
                if ($profile_value !== $current_user_value) {
                    $update_data[$user_field] = $profile_value;
                    update_user_meta($user_id, "_{$user_field}_modified", time());
                    $needs_update = true;
                }
            }
            
            if ($needs_update) {
                wp_update_user($update_data);
                error_log("HVAC Profile Sync: Profile {$post_id} synced to user {$user_id}");
            }
            
        } catch (Exception $e) {
            error_log("HVAC Profile Sync Error: " . $e->getMessage());
        } finally {
            unset(self::$sync_in_progress[$sync_key]);
        }
    }
    
    public function handle_concurrent_update($user_id, $profile_id, $field, $user_value, $profile_value, $timestamp) {
        // Conflict resolution: most recent update wins
        $user_field = str_replace('trainer_', '', $field);
        $user_modified = get_user_meta($user_id, "_{$user_field}_modified", true);
        $profile_modified = get_post_meta($profile_id, "_{$field}_modified", true);
        
        if ($user_modified > $profile_modified) {
            // User data is more recent, sync to profile
            update_post_meta($profile_id, $field, $user_value);
            update_post_meta($profile_id, "_{$field}_modified", $timestamp);
        } else {
            // Profile data is more recent, sync to user
            $user_update = ['ID' => $user_id, $user_field => $profile_value];
            wp_update_user($user_update);
            update_user_meta($user_id, "_{$user_field}_modified", $timestamp);
        }
        
        // Log conflict resolution
        error_log("HVAC Sync Conflict Resolved: Field '{$field}' for user {$user_id}");
    }
    
    public function schedule_sync_verification() {
        if (!wp_next_scheduled('hvac_verify_sync_integrity')) {
            wp_schedule_event(time(), 'hourly', 'hvac_verify_sync_integrity');
        }
    }
    
    public function verify_sync_integrity() {
        $profiles = get_posts([
            'post_type' => 'trainer_profile',
            'posts_per_page' => -1,
            'meta_query' => [
                [
                    'key' => 'user_id',
                    'compare' => 'EXISTS'
                ]
            ]
        ]);
        
        $sync_issues = [];
        
        foreach ($profiles as $profile) {
            $user_id = get_post_meta($profile->ID, 'user_id', true);
            $user = get_userdata($user_id);
            
            if (!$user) {
                $sync_issues[] = [
                    'type' => 'orphaned_profile',
                    'profile_id' => $profile->ID,
                    'user_id' => $user_id
                ];
                continue;
            }
            
            // Check field synchronization
            $sync_fields = [
                'first_name' => 'trainer_first_name',
                'last_name' => 'trainer_last_name',
                'display_name' => 'trainer_display_name'
            ];
            
            foreach ($sync_fields as $user_field => $profile_field) {
                $user_value = $user->$user_field;
                $profile_value = get_post_meta($profile->ID, $profile_field, true);
                
                if ($user_value !== $profile_value) {
                    $sync_issues[] = [
                        'type' => 'field_mismatch',
                        'profile_id' => $profile->ID,
                        'user_id' => $user_id,
                        'field' => $user_field,
                        'user_value' => $user_value,
                        'profile_value' => $profile_value
                    ];
                }
            }
        }
        
        if (!empty($sync_issues)) {
            $this->repair_sync_issues($sync_issues);
        }
    }
    
    private function repair_sync_issues($issues) {
        foreach ($issues as $issue) {
            switch ($issue['type']) {
                case 'orphaned_profile':
                    // Handle orphaned profiles
                    wp_update_post([
                        'ID' => $issue['profile_id'],
                        'post_status' => 'draft'
                    ]);
                    add_post_meta($issue['profile_id'], '_sync_status', 'orphaned');
                    break;
                    
                case 'field_mismatch':
                    // Auto-repair field mismatches (user data takes precedence)
                    update_post_meta(
                        $issue['profile_id'], 
                        'trainer_' . $issue['field'], 
                        $issue['user_value']
                    );
                    break;
            }
        }
        
        // Log repair actions
        error_log("HVAC Sync Repair: Fixed " . count($issues) . " sync issues");
    }
    
    public function force_sync_user_to_profile($user_id) {
        $profile_id = get_user_meta($user_id, 'trainer_profile_id', true);
        if (!$profile_id) {
            return false;
        }
        
        $user = get_userdata($user_id);
        if (!$user) {
            return false;
        }
        
        // Force sync all shared fields
        $sync_fields = [
            'first_name' => 'trainer_first_name',
            'last_name' => 'trainer_last_name',
            'display_name' => 'trainer_display_name'
        ];
        
        foreach ($sync_fields as $user_field => $profile_field) {
            update_post_meta($profile_id, $profile_field, $user->$user_field);
            update_post_meta($profile_id, "_{$profile_field}_modified", time());
        }
        
        return true;
    }
    
    public function force_sync_profile_to_user($profile_id) {
        $user_id = get_post_meta($profile_id, 'user_id', true);
        if (!$user_id) {
            return false;
        }
        
        // Force sync all shared fields
        $sync_fields = [
            'trainer_first_name' => 'first_name',
            'trainer_last_name' => 'last_name',
            'trainer_display_name' => 'display_name'
        ];
        
        $update_data = ['ID' => $user_id];
        
        foreach ($sync_fields as $profile_field => $user_field) {
            $profile_value = get_post_meta($profile_id, $profile_field, true);
            $update_data[$user_field] = $profile_value;
            update_user_meta($user_id, "_{$user_field}_modified", time());
        }
        
        wp_update_user($update_data);
        return true;
    }
    
    public function get_sync_status($user_id) {
        $profile_id = get_user_meta($user_id, 'trainer_profile_id', true);
        if (!$profile_id) {
            return ['status' => 'no_profile'];
        }
        
        $user = get_userdata($user_id);
        if (!$user) {
            return ['status' => 'invalid_user'];
        }
        
        $sync_fields = [
            'first_name' => 'trainer_first_name',
            'last_name' => 'trainer_last_name',
            'display_name' => 'trainer_display_name'
        ];
        
        $mismatches = [];
        foreach ($sync_fields as $user_field => $profile_field) {
            $user_value = $user->$user_field;
            $profile_value = get_post_meta($profile_id, $profile_field, true);
            
            if ($user_value !== $profile_value) {
                $mismatches[] = [
                    'field' => $user_field,
                    'user_value' => $user_value,
                    'profile_value' => $profile_value
                ];
            }
        }
        
        return [
            'status' => empty($mismatches) ? 'synced' : 'out_of_sync',
            'mismatches' => $mismatches,
            'profile_id' => $profile_id
        ];
    }
}

// Initialize the sync handler
HVAC_Profile_Sync_Handler::get_instance();