<?php
/**
 * Plugin Name:       PlanWise Floor Planner
 * Plugin URI:        https://planwisewp.com
 * Description:       An intuitive online floor plan creator with automatic measurements and calculations for WordPress.
 * Version:           1.0.0
 * Author:            ClaimPro Bt.
 * Author URI:        https://claimpro.hu
 * License:           GPLv2 or later
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       planwise-floor-planner
 * Domain Path:       /languages
 */

/* phpcs:disable WordPress.NamingConventions.PrefixAllGlobals */
/* phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound */
if ( ! defined( 'ABSPATH' ) ) {
    exit; 
}

define('PLANWISEWP_VERSION', '1.0.0');
define('PLANWISEWP_PLUGIN_FILE', __FILE__);
define('PLANWISEWP_PLUGIN_PATH', plugin_dir_path(__FILE__));
define('PLANWISEWP_PLUGIN_URL', plugin_dir_url(__FILE__));
define('PLANWISEWP_APP_PATH', PLANWISEWP_PLUGIN_PATH); 

if ( function_exists( 'pwfp_fs' ) ) {
    pwfp_fs()->set_basename( false, __FILE__ );
} else {

    if ( ! function_exists( 'pwfp_fs' ) ) {
        function pwfp_fs() {
            global $pwfp_fs;

            if ( ! isset( $pwfp_fs ) ) {

                $freemius_sdk_path = dirname( __FILE__ ) . '/vendor/freemius/start.php';

                if ( file_exists( $freemius_sdk_path ) ) {
                    require_once $freemius_sdk_path; 

                    try {
                        $pwfp_fs = fs_dynamic_init( array(
                            'id'                  => '21289', 
                            'slug'                => 'planwise-floor-planner', 
                            'premium_slug'        => 'planwise-floor-planner-pro',
                            'type'                => 'plugin',
                            'public_key'          => 'pk_091e3eef4827e68d4aeca326ae560', 
                            'is_premium'          => false, 
                            'has_premium_version' => true, 
                            'has_addons'          => false,
                            'has_paid_plans'      => true, 
                            'is_org_compliant'    => true, 
                            'menu'                => array(
                                'slug'           => 'planwisewp-admin', 
                                'support'        => false, 
                                 'account'     => true, 
                                 'pricing'     => true, 
                            ),
                            'is_live'             => true,
                        ) );
                    } catch (Freemius_Exception $e) {
                        add_action('admin_notices', function() use ($e) {
                            /* translators: %s: error message */
                            echo '<div class="notice notice-error"><p>' . sprintf(esc_html__('PlanWiseWP Freemius SDK failed: %s Please contact support.', 'planwise-floor-planner'), esc_html($e->getMessage())) . '</p></div>';
                        });
                         $pwfp_fs = new stdClass();
                         $pwfp_fs->is_free_plan = function() { return true; }; 
                         $pwfp_fs->add_filter = function() {}; 
                         $pwfp_fs->add_action = function() {};
                    }
                } else {
                     add_action('admin_notices', function() use ($freemius_sdk_path) {
                        /* translators: %s: file path */
                         echo '<div class="notice notice-error"><p>' . sprintf(esc_html__('PlanWiseWP error: Freemius SDK not found at %s. Please ensure the vendor folder is correctly placed.', 'planwise-floor-planner'), esc_html($freemius_sdk_path)) . '</p></div>';
                     });
                      $pwfp_fs = new stdClass();
                      $pwfp_fs->is_free_plan = function() { return true; };
                      $pwfp_fs->add_filter = function() {};
                      $pwfp_fs->add_action = function() {};
                }
            }

            return $pwfp_fs;
        }

        pwfp_fs();
        do_action( 'pwfp_fs_loaded' );
        
        if ( function_exists('pwfp_fs') ) {
            pwfp_fs()->add_action('after_uninstall', 'planwisewp_fs_uninstall_cleanup');
        }
    }

    function planwisewp_fs_uninstall_cleanup() {
        global $wpdb;
        if ( ! current_user_can( 'delete_plugins' ) ) {
            return;
        }
        
        $wpdb->query( "DROP TABLE IF EXISTS `" . $wpdb->prefix . "planwisewp_projects`" );

        $options_like = 'planwisewp_%';
        $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", $options_like ) );
    }
    
    function planwisewp_is_pro() {
        if (defined('PLANWISEWP_DEV_FORCE_PRO') && PLANWISEWP_DEV_FORCE_PRO) {
            return true; 
        }

        if ( function_exists( 'pwfp_fs' ) ) {
            global $pwfp_fs;
            if ( isset( $pwfp_fs ) && method_exists( $pwfp_fs, 'is_free_plan' ) ) {
                 return !$pwfp_fs->is_free_plan();
            }
        }

        return false; 
    }

    // Helper function to sanitize project data
    function planwisewp_sanitize_project_data($data) {
        if (!is_array($data)) return [];
        
        $clean_data = [];
        
        // Sanitize Rooms
        if (isset($data['rooms']) && is_array($data['rooms'])) {
            $clean_data['rooms'] = [];
            foreach ($data['rooms'] as $room) {
                $clean_room = [];
                $clean_room['id'] = isset($room['id']) ? sanitize_text_field($room['id']) : '';
                $clean_room['x'] = isset($room['x']) ? floatval($room['x']) : 0;
                $clean_room['y'] = isset($room['y']) ? floatval($room['y']) : 0;
                $clean_room['width'] = isset($room['width']) ? floatval($room['width']) : 0;
                $clean_room['height'] = isset($room['height']) ? floatval($room['height']) : 0;
                $clean_room['name'] = isset($room['name']) ? sanitize_text_field($room['name']) : '';
                $clean_room['color'] = isset($room['color']) ? sanitize_hex_color($room['color']) : '';
                $clean_room['tiledHeight'] = isset($room['tiledHeight']) ? floatval($room['tiledHeight']) : 0;
                $clean_data['rooms'][] = $clean_room;
            }
        }

        // Sanitize Markers
        if (isset($data['markers']) && is_array($data['markers'])) {
            $clean_data['markers'] = [];
            foreach ($data['markers'] as $marker) {
                $clean_marker = [];
                $clean_marker['id'] = isset($marker['id']) ? sanitize_text_field($marker['id']) : '';
                $clean_marker['type'] = isset($marker['type']) ? sanitize_key($marker['type']) : '';
                $clean_marker['x'] = isset($marker['x']) ? floatval($marker['x']) : 0;
                $clean_marker['y'] = isset($marker['y']) ? floatval($marker['y']) : 0;
                $clean_marker['width'] = isset($marker['width']) ? floatval($marker['width']) : 0;
                $clean_marker['height'] = isset($marker['height']) ? floatval($marker['height']) : 0;
                $clean_marker['parentRoomId'] = isset($marker['parentRoomId']) ? sanitize_text_field($marker['parentRoomId']) : null;
                $clean_marker['linkedRoomId'] = isset($marker['linkedRoomId']) ? sanitize_text_field($marker['linkedRoomId']) : null;
                $clean_marker['edge'] = isset($marker['edge']) ? sanitize_key($marker['edge']) : '';
                $clean_marker['orientation'] = isset($marker['orientation']) ? sanitize_key($marker['orientation']) : '';
                $clean_marker['parapetHeight'] = isset($marker['parapetHeight']) ? floatval($marker['parapetHeight']) : 0;
                $clean_data['markers'][] = $clean_marker;
            }
        }

        // Sanitize Fillers
        if (isset($data['fillers']) && is_array($data['fillers'])) {
            $clean_data['fillers'] = [];
            foreach ($data['fillers'] as $filler) {
                $clean_filler = [];
                $clean_filler['id'] = isset($filler['id']) ? sanitize_text_field($filler['id']) : '';
                $clean_filler['x'] = isset($filler['x']) ? floatval($filler['x']) : 0;
                $clean_filler['y'] = isset($filler['y']) ? floatval($filler['y']) : 0;
                $clean_filler['width'] = isset($filler['width']) ? floatval($filler['width']) : 0;
                $clean_filler['height'] = isset($filler['height']) ? floatval($filler['height']) : 0;
                $clean_data['fillers'][] = $clean_filler;
            }
        }

        // Sanitize Global Settings
        $clean_data['ceilingHeight'] = isset($data['ceilingHeight']) ? floatval($data['ceilingHeight']) : 0;
        $clean_data['wallThickness'] = isset($data['wallThickness']) ? floatval($data['wallThickness']) : 0;
        
        if (isset($data['view']) && is_array($data['view'])) {
            $clean_data['view'] = [
                'x' => isset($data['view']['x']) ? floatval($data['view']['x']) : 0,
                'y' => isset($data['view']['y']) ? floatval($data['view']['y']) : 0,
                'scale' => isset($data['view']['scale']) ? floatval($data['view']['scale']) : 1,
            ];
        }

        return $clean_data;
    }

    // --- FRONTEND ASSETS ---
    if (!function_exists('planwisewp_enqueue_assets')) {
        function planwisewp_enqueue_assets() {
            global $post;
            if (!is_singular() || !is_a($post, 'WP_Post') || !has_shortcode($post->post_content, 'planwisewp')) {
                return;
            }

            // 1. Main CSS
            wp_enqueue_style(
                'planwisewp-style', 
                PLANWISEWP_PLUGIN_URL . 'css/style.css', 
                [], 
                filemtime(PLANWISEWP_APP_PATH . 'css/style.css') 
            );

            // 2. Dynamic CSS
            $planwisewp_default_colors = [
                'add_room' => '#17a2b8', 'filler' => '#17a2b8', 'zoom' => '#6c757d',
                'hamburger' => '#664BC4',
                'marker_dot' => '#dc3545', 'marker_window' => '#17a2b8', 'marker_door' => '#17a2b8', 'marker_opening' => '#17a2b8',
                'export_png' => '#28a745', 'export_txt' => '#28a745', 'export_csv' => '#28a745',
                'project_save' => '#ffc107', 'project_load' => '#ffc107',
                'edit_rotate' => '#17a2b8', 'edit_delete' => '#dc3545'
            ];
            $button_colors = wp_parse_args(get_option('planwisewp_button_colors', []), $planwisewp_default_colors);
            
            $custom_css = ":root {
                --pw-btn-add_room: " . esc_attr($button_colors['add_room']) . ";
                --pw-btn-filler: " . esc_attr($button_colors['filler']) . ";
                --pw-btn-zoom: " . esc_attr($button_colors['zoom']) . ";
                --pw-btn-hamburger: " . esc_attr($button_colors['hamburger']) . ";
                --pw-btn-marker_dot: " . esc_attr($button_colors['marker_dot']) . ";
                --pw-btn-marker_window: " . esc_attr($button_colors['marker_window']) . ";
                --pw-btn-marker_door: " . esc_attr($button_colors['marker_door']) . ";
                --pw-btn-marker_opening: " . esc_attr($button_colors['marker_opening']) . ";
                --pw-btn-export_png: " . esc_attr($button_colors['export_png']) . ";
                --pw-btn-export_txt: " . esc_attr($button_colors['export_txt']) . ";
                --pw-btn-export_csv: " . esc_attr($button_colors['export_csv']) . ";
                --pw-btn-project_save: " . esc_attr($button_colors['project_save']) . ";
                --pw-btn-project_load: " . esc_attr($button_colors['project_load']) . ";
                --pw-btn-edit_rotate: " . esc_attr($button_colors['edit_rotate']) . ";
                --pw-btn-edit_delete: " . esc_attr($button_colors['edit_delete']) . ";
            }";
            wp_add_inline_style('planwisewp-style', $custom_css);

            // 3. Icons (Fontello)
            wp_enqueue_style(
                'planwisewp-icons', 
                PLANWISEWP_PLUGIN_URL . 'assets/icons/css/fontello.css', 
                [], 
                '1.0.0' 
            );

            // 4. Main JS
            wp_enqueue_script(
                'planwisewp-js', 
                PLANWISEWP_PLUGIN_URL . 'js/pw.js', 
                [], 
                filemtime(PLANWISEWP_APP_PATH . 'js/pw.js'), 
                true 
            );
            
            wp_localize_script('planwisewp-js', 'planwisewp_ajax_obj', [
                'ajax_url' => admin_url('admin-ajax.php'), 
                'nonce'    => wp_create_nonce('planwisewp_ajax_nonce'), 
                'is_pro'   => planwisewp_is_pro(), 
                'unit_system' => get_option('planwisewp_unit_system', 'metric'),
                'marker_color' => get_option('planwisewp_marker_color', '#dc3545'),
                'free_defaults' => [
                    'window_w' => (int) get_option('planwisewp_free_window_width', '100'),
                    'window_h' => (int) get_option('planwisewp_free_window_height', '120'),
                    'door_w'   => (int) get_option('planwisewp_free_door_width', '80'),
                    'door_h'   => (int) get_option('planwisewp_free_door_height', '210'),
                ],

                'l10n' => [ 
                     /* translators: %d: number of rooms */
                     'confirm_delete' => __( 'Are you sure you want to delete the selected item?', 'planwise-floor-planner' ),
                     'no_selection_delete' => __( 'No item selected for deletion.', 'planwise-floor-planner' ),
                     'invalid_json' => __( 'ERROR: The file is corrupt or has an invalid format. Check console for details.', 'planwise-floor-planner' ),
                     'file_read_error' => __( 'ERROR: Failed to read the file.', 'planwise-floor-planner' ),
                     'nothing_to_export' => __( 'There is nothing to export!', 'planwise-floor-planner' ),
                     'error_saving_background' => __( 'An error occurred during background saving, but the download will start.', 'planwise-floor-planner' ),
                     'network_error_saving' => __( 'A network error occurred during background saving, but the download will start.', 'planwise-floor-planner' ),
                     
                     /* translators: 1: opening width, 2: max width */
                     'opening_too_wide' => __( 'Warning: The opening (%1$s) is too wide! Maximum width on this wall section is %2$s.', 'planwise-floor-planner' ),
                     'window_on_shared_wall' => __( 'Warning: You cannot place a window on a shared wall!', 'planwise-floor-planner' ),
                     
                     /* translators: 1: opening height, 2: ceiling height */
                     'opening_height_too_large' => __( 'The opening height (%1$s) cannot be greater than or equal to the ceiling height (%2$s).', 'planwise-floor-planner' ),
                     'ceilingHeight' => __( 'Ceiling Height', 'planwise-floor-planner' ),
                     'no_rooms' => __( 'No rooms on the floor plan yet.', 'planwise-floor-planner' ),
                     'generalData' => __( 'General Data', 'planwise-floor-planner' ),
                     'roomCount' => __( 'Room Count', 'planwise-floor-planner' ),
                     'totalNetFloorArea' => __( 'Total Net Floor Area', 'planwise-floor-planner' ),
                     'totalGrossFloorArea' => __( 'Total Gross Floor Area', 'planwise-floor-planner' ),
                     'grossTotalSurface' => __( 'Total Gross Surface', 'planwise-floor-planner' ),
                     'totalOpenings' => __( 'Total Openings', 'planwise-floor-planner' ),
                     'windows' => __( 'Windows', 'planwise-floor-planner' ),
                     'doors' => __( 'Doors', 'planwise-floor-planner' ),
                     'pcs' => __( 'pcs', 'planwise-floor-planner' ),
                     'dimensions' => __( 'Dimensions', 'planwise-floor-planner' ),
                     'netFloorArea' => __( 'Net Floor Area', 'planwise-floor-planner' ),
                     'openings' => __( 'Openings', 'planwise-floor-planner' ),
                     'parapetHeight' => __( 'Sill Height', 'planwise-floor-planner' ),
                     'wallSurfaces' => __( 'Wall Surfaces', 'planwise-floor-planner' ),
                     'grossWallSurface' => __( 'Gross Wall Surface', 'planwise-floor-planner' ),
                     'totals' => __( 'Totals', 'planwise-floor-planner' ),
                     'close' => __( 'Close', 'planwise-floor-planner' ),
                     'loadingTooltip' => __( 'Loading...', 'planwise-floor-planner' ),
                     'watermarkText' => __( 'PlanWise Floor Planner', 'planwise-floor-planner' )
                ]
            ]);
        }
        add_action('wp_enqueue_scripts', 'planwisewp_enqueue_assets');
    }

    // --- ADMIN ASSETS ---
    if (!function_exists('planwisewp_enqueue_admin_assets')) {
        function planwisewp_enqueue_admin_assets($hook) {
            if (strpos($hook, 'planwisewp-admin') === false) {
                return;
            }

            wp_enqueue_style(
                'planwisewp-admin-style',
                PLANWISEWP_PLUGIN_URL . 'assets/css/admin-style.css',
                [],
                PLANWISEWP_VERSION
            );

            wp_enqueue_script(
                'planwisewp-admin-js',
                PLANWISEWP_PLUGIN_URL . 'assets/js/admin-pw.js',
                ['jquery'], 
                PLANWISEWP_VERSION,
                true
            );

            wp_localize_script('planwisewp-admin-js', 'planwisewpAdminParams', [
                'resetConfirm' => __('Are you absolutely sure you want to reset all PlanWiseWP settings to their defaults? This cannot be undone!', 'planwise-floor-planner')
            ]);
        }
        add_action('admin_enqueue_scripts', 'planwisewp_enqueue_admin_assets');
    }

    function planwisewp_get_start_of_period($period) {
        switch ($period) {
            case 'daily':
                return wp_date('Y-m-d 00:00:00');
            case 'weekly':
                return wp_date('Y-m-d 00:00:00', strtotime('last monday'));
            case 'monthly':
            default:
                return wp_date('Y-m-01 00:00:00');
        }
    }

    add_shortcode('planwisewp', 'planwisewp_shortcode'); 
        function planwisewp_shortcode() {
        if (!is_user_logged_in()) {
            $login_url = wp_login_url(get_permalink());
            
            $message = sprintf(
                '<div id="planwisewp-login-prompt" style="padding: 2rem; text-align: center; background-color: #fff; border-radius: 16px;"><h1>%1$s</h1><br><p style="text-align: justify;">%2$s</p><p>%3$s</p></div>',
                esc_html__('Welcome to the PlanWiseWP Online Floor Plan Creator!', 'planwise-floor-planner'),
                esc_html__('Login is required to use the floor plan and measurement report creator. With this tool, you can easily create scaled and dimensioned floor plans directly in your browser. You can save the scaled plan as an image.', 'planwise-floor-planner'),
                sprintf('<a href="%1$s" class="wp-element-button button planwisewp-login-button-spacing">%2$s</a>', esc_url($login_url), esc_html__('Login', 'planwise-floor-planner'))
            );

            $message = apply_filters('planwisewp_logged_out_message_html', $message); 

            $allowed_html = [
                'div' => ['id' => true, 'style' => true, 'class'=>true],
                'h1' => ['style' => true],
                'h3' => ['style' => true],
                'p' => ['style' => true],
                'a' => ['href' => true, 'style' => true, 'target' => true],
                'br' => [],
                'strong' => [],
                'b' => [],
                'em' => [],
                'i' => [],
                'iframe' => ['style'=>true, 'src'=>true, 'frameborder'=>true, 'allow'=>true, 'allowfullscreen'=>true],
            ];
            
            return '<div id="planwisewp-container"><div class="main-container">' . wp_kses($message, $allowed_html) . '</div></div>'; 
        }

        global $wpdb;
        $current_user = wp_get_current_user();
        $user_id = $current_user->ID;

        ob_start();
        echo '<div id="planwisewp-container"><div class="main-container alaprajz-mode">'; 
        $template_path = PLANWISEWP_APP_PATH . 'templates/step-pw.php';
        if (file_exists($template_path)) {
            include($template_path);
        } else {
            echo '<p>' . esc_html__('Error: Template file not found.', 'planwise-floor-planner') . '</p>';
        }
        echo '</div></div>';
        return ob_get_clean();
    }

    add_action('wp_ajax_planwisewp_save_project', 'planwisewp_handle_save_project_ajax'); 
    function planwisewp_handle_save_project_ajax() {
        check_ajax_referer('planwisewp_ajax_nonce', '_ajax_nonce'); 
        if (!is_user_logged_in()){ wp_send_json_error(['message' => __('Access denied.', 'planwise-floor-planner')], 403); }

        global $wpdb;
        $current_user_save = wp_get_current_user();
        $user_id_save = $current_user_save->ID;
        $projects_table_save = $wpdb->prefix . 'planwisewp_projects';

        if (!isset($_POST['projectId'])) {
             wp_send_json_error(['message' => __('Invalid project ID.', 'planwise-floor-planner')], 400);
        }
        $project_id_save = sanitize_text_field( wp_unslash( $_POST['projectId'] ) );
        if (!preg_match('/^proj_[a-z0-9]+$/', $project_id_save)) {
             wp_send_json_error(['message' => __('Invalid project ID format.', 'planwise-floor-planner')], 400);
        }

        $existing_project_owner = $wpdb->get_var($wpdb->prepare("SELECT user_id FROM {$wpdb->prefix}planwisewp_projects WHERE project_id = %s", $project_id_save));
        $is_editing_own = ($existing_project_owner && $existing_project_owner == $user_id_save);

        $user_name_save = trim($current_user_save->user_lastname . ' ' . $current_user_save->user_firstname);
        if (empty($user_name_save)) $user_name_save = $current_user_save->display_name;

        $project_json_raw = isset( $_POST['projectJson'] ) ? wp_unslash( $_POST['projectJson'] ) : null;
        if ( ! is_string( $project_json_raw ) ) {
             wp_send_json_error(['message' => __('Missing or invalid project data.', 'planwise-floor-planner')], 400);
        }
        $project_json_string_save = $project_json_raw; 
        $project_data_save = json_decode($project_json_string_save, true);
        if (json_last_error() !== JSON_ERROR_NONE || !is_array($project_data_save)) {
            wp_send_json_error(['message' => __('Invalid JSON data format. Please check console for details.', 'planwise-floor-planner')], 400);
        }

        // Sanitization (Validated)
        $sanitized_project_data = planwisewp_sanitize_project_data($project_data_save);
        $project_json_string_save = json_encode($sanitized_project_data);

        $rooms_count_save = isset($sanitized_project_data['rooms']) && is_array($sanitized_project_data['rooms']) ? count($sanitized_project_data['rooms']) : 0;
        $total_floor_area_save = 0;
        if ($rooms_count_save > 0) {
            foreach ($sanitized_project_data['rooms'] as $room) {
                if (isset($room['width']) && isset($room['height'])) {
                    $total_floor_area_save += (floatval($room['width']) * floatval($room['height'])) / 10000; 
                }
            }
        }

        $data_to_save = [
            'user_id' => $user_id_save, 
            'user_name' => $user_name_save, 
            'user_email' => $current_user_save->user_email,
            'project_json' => $project_json_string_save,
            'rooms_count' => $rooms_count_save, 
            'total_floor_area' => $total_floor_area_save,
        ];

        $existing_id = $wpdb->get_var($wpdb->prepare("SELECT id FROM {$wpdb->prefix}planwisewp_projects WHERE project_id = %s", $project_id_save));

        if ($existing_id) {
             $result = $wpdb->update(
                $projects_table_save, 
                $data_to_save,
                ['project_id' => $project_id_save, 'user_id' => $user_id_save],
                ['%d', '%s', '%s', '%s', '%d', '%f'],
                ['%s', '%d'] 
             );
            if ($result === false) { 
                 wp_send_json_error(['message' => __('Database error during update.', 'planwise-floor-planner')], 500);
            } else {
                 if ($result === 0 && !$is_editing_own) { 
                      wp_send_json_error(['message' => __('Failed to update. You might not own this project.', 'planwise-floor-planner')], 403);
                 } else {
                     wp_send_json_success(['message' => __('Floor plan updated successfully.', 'planwise-floor-planner'),
                     ]);
                 }
            }
        } else {
            $data_to_save['project_id'] = $project_id_save;
            $data_to_save['created_at'] = current_time('mysql');
            $result = $wpdb->insert($projects_table_save, $data_to_save, ['%d', '%s', '%s', '%s', '%d', '%f', '%s', '%s']); 

            if ($result === false) {
                 wp_send_json_error(['message' => __('Database error during insert.', 'planwise-floor-planner')], 500);
            } else {
                 wp_send_json_success([
                     'message' => __('Floor plan saved successfully.', 'planwise-floor-planner'),
                 ]);
            }
        }
    }

    if ( is_admin() ) {
        $planwisewp_admin_file_path = PLANWISEWP_APP_PATH . 'admin/pw_admin.php';
        if ( file_exists($planwisewp_admin_file_path) ) {
            require_once($planwisewp_admin_file_path);
        } else {
             add_action('admin_notices', function() use ($planwisewp_admin_file_path) {
                 /* translators: %s: file path */
                 echo '<div class="notice notice-error"><p>' . sprintf(esc_html__('PlanWiseWP error: Admin file not found at %s.', 'planwise-floor-planner'), esc_html($planwisewp_admin_file_path)) . '</p></div>';
             });
        }

         function planwisewp_add_admin_menu() {
             add_menu_page(__('PlanWiseWP Admin', 'planwise-floor-planner'), __('PlanWiseWP', 'planwise-floor-planner'), 'manage_options', 'planwisewp-admin', 'planwisewp_render_admin_page', 'dashicons-edit-page', 75);
         }
         add_action('admin_menu', 'planwisewp_add_admin_menu');
    }

    $planwisewp_install_file_path = PLANWISEWP_APP_PATH . 'includes/pw_install.php';
    if ( file_exists($planwisewp_install_file_path) ) {
        require_once($planwisewp_install_file_path);
        register_activation_hook(PLANWISEWP_PLUGIN_FILE, 'planwisewp_install');
    } else {
         add_action('admin_notices', function() use ($planwisewp_install_file_path) {
             /* translators: %s: file path */
             echo '<div class="notice notice-error"><p>' . sprintf(esc_html__('PlanWiseWP error: Install file not found at %s.', 'planwise-floor-planner'), esc_html($planwisewp_install_file_path)) . '</p></div>';
         });
    }

}
?>