<?php
if (!defined('ABSPATH')) exit;

/**
 * Compatibility facade for older callers.
 * Full website generation is owned by VMO_Canonical_Website_Pipeline.
 */
class VMO_Website_Generator {

    public function generate_complete_website($user_id, $consultation_data, $research_data = null) {
        if (!class_exists('VMO_Canonical_Website_Pipeline')) {
            return new WP_Error('vmo_canonical_pipeline_missing', 'Canonical website pipeline is required for website generation.');
        }

        $payload = is_array($consultation_data)
            ? $consultation_data
            : array('consultation_data' => $consultation_data);

        if (is_array($research_data)) {
            $payload['research_data'] = $research_data;
        }

        $payload['legacy_generator_user_id'] = absint($user_id);
        $session_id = $this->resolve_session_id($payload);

        return VMO_Canonical_Website_Pipeline::get_instance()->run_from_consultation_payload($session_id, $payload);
    }

    private function resolve_session_id(array $payload) {
        foreach (array('session_id', 'consultation_session_id', 'id') as $session_key) {
            if (!empty($payload[$session_key]) && is_scalar($payload[$session_key])) {
                return sanitize_text_field((string) $payload[$session_key]);
            }
        }

        return 'vmo_legacy_generator_' . wp_generate_uuid4();
    }
}