<?php
/**
 * PlanWiseWP Plugin Installer
 * Creates the necessary database tables and sets default options on activation.
 */

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

/**
 * Main installation function, runs on plugin activation.
 */
function planwisewp_install() {
    global $wpdb;
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

    $charset_collate = $wpdb->get_charset_collate(); 
    $table_name_projects = $wpdb->prefix . 'planwisewp_projects';
    $sql_projects = "CREATE TABLE $table_name_projects (
        id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
        project_id varchar(32) NOT NULL,
        user_id bigint(20) unsigned NOT NULL,
        user_name varchar(255) NOT NULL,
        user_email varchar(255) NOT NULL,
        created_at datetime NOT NULL,
        project_json longtext DEFAULT NULL,
        rooms_count int(11) DEFAULT 0,
        total_floor_area float DEFAULT 0,
        PRIMARY KEY (id),
        UNIQUE KEY project_id (project_id),
        KEY user_id (user_id),                   
        KEY created_at (created_at)             
    ) $charset_collate;";
    dbDelta($sql_projects);

    add_option('planwisewp_unit_system', 'metric'); 
    add_option('planwisewp_marker_color', '#dc3545');

    $default_page_title = __('PlanWiseWP online floor plan and measurement report creation', 'planwise-floor-planner');
    $default_tooltip_title = __('Usage Guide', 'planwise-floor-planner');
    $default_tooltip_content = sprintf(
        '<h4>%1$s</h4><p><strong>%2$s:</strong> %3$s</p><p><strong>%4$s:</strong> %5$s</p><p><strong>%6$s:</strong> %7$s</p><p><strong>%8$s:</strong> %9$s</p><p><strong>%10$s:</strong> %11$s</p><p><strong>%12$s:</strong> %13$s</p><p><strong>%14$s:</strong> %15$s</p><p><strong>%16$s:</strong> %17$s</p>',
        esc_html__('Usage guide', 'planwise-floor-planner'),
        esc_html__('Basic settings', 'planwise-floor-planner'),
        esc_html__('Enter ceiling height. Enter the thinnest common wall thickness (critical for magnetic snapping).', 'planwise-floor-planner'),
        esc_html__('Add elements', 'planwise-floor-planner'),
        esc_html__('Select room or pillar/filler, enter dimensions (W/L), then click the corresponding \'+\' button.', 'planwise-floor-planner'),
        esc_html__('Select & move', 'planwise-floor-planner'),
        esc_html__('Left-click (or tap) to select an element. Click-and-drag (or touch-and-drag) to move. Elements snap magnetically.', 'planwise-floor-planner'),
        esc_html__('Edit functions', 'planwise-floor-planner'),
        esc_html__('Right-click (or long-press after tap) on a selected element to open the edit panel for resize, rotate and delete options.', 'planwise-floor-planner'),
        esc_html__('Connect walls', 'planwise-floor-planner'),
        esc_html__('Drag rooms close to snap them. Ideal gap for shared walls is 0 to 2x the wall thickness (PRO shows gap visually). Avoid overlaps.', 'planwise-floor-planner'),
        esc_html__('Add Openings', 'planwise-floor-planner'),
        esc_html__('Set size (custom in PRO, fixed in Free), click \'+\' (Door/Window/Opening), then click on the target wall segment. PRO: Set sill height for windows.', 'planwise-floor-planner'),
        esc_html__('Other tools & PRO', 'planwise-floor-planner'),
        esc_html__('Click \'!\' to add note markers (PRO: custom color). PRO: Specify tiled height per room, customize room colors, see on-canvas dimensions.', 'planwise-floor-planner'),
        esc_html__('Navigate & save/Export', 'planwise-floor-planner'),
        esc_html__('Right-drag or two-finger drag to pan. Scroll, pinch, or use +/- buttons to zoom. Export PNG (watermarked in Free). PRO: Save/Load projects, export TXT/CSV, no/custom watermark.', 'planwise-floor-planner')
    );
    
    $default_logged_out_message = sprintf(
        '<h1>%1$s</h1><br><p style="text-align: justify;">%2$s</p><p style="text-align: justify;">%3$s</p><p>%4$s</p>', 
        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 for various purposes like home renovation planning, real estate listings, interior design mockups, facility management, or insurance claims documentation. You can save the scaled plan as an image (PRO allows watermark removal and advanced export formats).', 'planwise-floor-planner'),
        esc_html__('The online floor plan creator is free for personal use. Please log in to use the floor plan creator application!', 'planwise-floor-planner'),
        esc_html__('Please log in to use the PlanWiseWP application!', 'planwise-floor-planner')
    );

    $default_rooms = [
        'Living Room', 'Kitchen', 'Dining Room', 'Bathroom', 'Bedroom 1', 'Bedroom 2', 'Kids Room', 'Office', 'Hallway', 'Corridor', 'Anteroom', 'Walk-in Closet', 'Pantry', 'WC', 'Garage', 'Balcony', 'Basement', 'Storage', 'Utility Room'
    ];
    $default_rooms_json = json_encode($default_rooms);

    add_option('planwisewp_page_title', $default_page_title);
    add_option('planwisewp_tooltip_title', $default_tooltip_title);
    add_option('planwisewp_tooltip_content', $default_tooltip_content);
    add_option('planwisewp_logged_out_message', $default_logged_out_message);
    add_option('planwisewp_room_list', $default_rooms_json);
    add_option('planwisewp_free_window_width', '100');
    add_option('planwisewp_free_window_height', '120');
    add_option('planwisewp_free_door_width', '80');
    add_option('planwisewp_free_door_height', '210');
    $default_button_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'
    ];
    add_option('planwisewp_button_colors', $default_button_colors);
}
?>