<?php
defined('BASEPATH') or exit('No direct script access allowed');

/**
 * Demo Landing Page Testing Controller
 * Use this to easily test the demo landing page feature
 */
class Demo_test extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Test the demo landing page
     * Visit: yoursite.com/demo_test
     */
    public function index()
    {
        echo "<h1>Demo Landing Page Testing</h1>";
        echo "<hr>";
        
        // Check current demo status
        $is_demo = is_demo();
        $demo_entered = $this->session->userdata('demo_entered');
        
        echo "<h2>Current Status:</h2>";
        echo "<ul>";
        echo "<li><strong>Demo Mode:</strong> " . ($is_demo == 1 ? 'ENABLED ✓' : 'DISABLED ✗') . "</li>";
        echo "<li><strong>Demo Entered:</strong> " . ($demo_entered ? 'YES (User has clicked Go to Site)' : 'NO (Landing page will show)') . "</li>";
        echo "</ul>";
        
        echo "<h2>Test Actions:</h2>";
        echo "<p><a href='" . base_url() . "' style='display:inline-block; padding:10px 20px; background:#667eea; color:white; text-decoration:none; border-radius:5px; margin:5px;'>View Homepage</a></p>";
        echo "<p><a href='" . base_url('demo_test/reset') . "' style='display:inline-block; padding:10px 20px; background:#f56565; color:white; text-decoration:none; border-radius:5px; margin:5px;'>Reset Landing Page Session</a></p>";
        echo "<p><a href='" . base_url('demo_test/toggle') . "' style='display:inline-block; padding:10px 20px; background:#48bb78; color:white; text-decoration:none; border-radius:5px; margin:5px;'>Toggle Demo Mode</a></p>";
        
        echo "<h2>How to Test:</h2>";
        echo "<ol>";
        echo "<li>Make sure Demo Mode is ENABLED</li>";
        echo "<li>Click 'Reset Landing Page Session'</li>";
        echo "<li>Click 'View Homepage' - you should see the landing page</li>";
        echo "<li>Click the 'Go to Site' button on the landing page</li>";
        echo "<li>You should now see the normal homepage</li>";
        echo "<li>Refresh - you should still see the normal homepage (not the landing page)</li>";
        echo "</ol>";
        
        echo "<h2>Notes:</h2>";
        echo "<ul>";
        echo "<li>The landing page only shows when is_demo()==1</li>";
        echo "<li>Once you click 'Go to Site', a session is set to remember you've entered</li>";
        echo "<li>The session persists until browser close or you reset it</li>";
        echo "<li>This is perfect for Envato demos where AJAX doesn't work in iframes</li>";
        echo "</ul>";
    }

    /**
     * Reset the demo landing page session
     * Visit: yoursite.com/demo_test/reset
     */
    public function reset()
    {
        reset_demo_landing();
        $this->session->set_flashdata('success', 'Demo landing page session has been reset!');
        redirect('demo_test/index');
    }

    /**
     * Toggle demo mode (for testing only - in production, use settings)
     * Visit: yoursite.com/demo_test/toggle
     */
    public function toggle()
    {
        $this->load->model('admin_m');
        $settings = settings();
        
        // Toggle environment setting
        if (isset($settings['environment']) && $settings['environment'] == 'demo') {
            $new_value = 'production';
            $message = 'Demo mode has been DISABLED';
        } else {
            $new_value = 'demo';
            $message = 'Demo mode has been ENABLED';
        }
        
        // Update settings
        $data = array('environment' => $new_value);
        $this->db->where('id', 1);
        $this->db->update('settings', $data);
        
        // Clear cache if you have any
        $this->session->set_flashdata('success', $message);
        
        redirect('demo_test/index');
    }
}
