<?php
// admin/facebook-test.php
require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/facebook-pixel.php';

if (!isAdmin()) {
    header('Location: login.php');
    exit;
}

$test_result = '';
$event_response = '';

if (isset($_POST['test_event'])) {
    $event_name = $_POST['event_name'] ?? 'PageView';
    $test_data = [
        'currency' => 'BDT', 
        'value' => 100, 
        'content_name' => 'Test Product',
        'content_id' => 'TEST-001',
        'content_type' => 'product'
    ];
    $user_data = [
        'url' => 'https://test.com/',
        'email' => 'test@example.com',
        'phone' => '01712345678'
    ];
    
    // Send test event
    $result = sendFacebookCAPI($event_name, $user_data, $test_data);
    $test_result = $result ? '✅ Event sent successfully!' : '❌ Failed to send event';
    
    // Get the response for debugging
    $event_response = $result ? 'Event sent to Facebook' : 'Check error logs for details';
}

include 'header.php';
?>

<div class="admin-card">
    <h5 class="card-title">Facebook Pixel & CAPI Test Tool</h5>
    
    <?php if ($test_result): ?>
    <div class="alert alert-<?php echo strpos($test_result, '✅') !== false ? 'success' : 'danger'; ?>">
        <?php echo $test_result; ?>
        <?php if ($event_response): ?>
        <br><small><?php echo $event_response; ?></small>
        <?php endif; ?>
    </div>
    <?php endif; ?>
    
    <div class="row">
        <div class="col-md-6">
            <div class="card p-3 mb-3">
                <h6>Pixel Status</h6>
                <?php if (FACEBOOK_PIXEL_ID): ?>
                <p class="text-success">✅ Pixel ID: <?php echo FACEBOOK_PIXEL_ID; ?></p>
                <?php else: ?>
                <p class="text-danger">❌ Pixel ID not configured</p>
                <small>Go to Settings to add your Pixel ID</small>
                <?php endif; ?>
            </div>
        </div>
        <div class="col-md-6">
            <div class="card p-3 mb-3">
                <h6>CAPI Status</h6>
                <?php if (FACEBOOK_CAPI_ENABLED && FACEBOOK_ACCESS_TOKEN): ?>
                <p class="text-success">✅ CAPI Enabled</p>
                <?php else: ?>
                <p class="text-danger">❌ CAPI not configured</p>
                <small>Enable CAPI in Settings</small>
                <?php endif; ?>
            </div>
        </div>
    </div>
    
    <form method="POST" class="mt-3">
        <div class="row">
            <div class="col-md-4">
                <label class="form-label">Event Name</label>
                <select name="event_name" class="form-control">
                    <option value="PageView">PageView</option>
                    <option value="ViewContent">ViewContent</option>
                    <option value="AddToCart">AddToCart</option>
                    <option value="InitiateCheckout">InitiateCheckout</option>
                    <option value="Purchase">Purchase</option>
                    <option value="Lead">Lead</option>
                    <option value="CompleteRegistration">CompleteRegistration</option>
                </select>
            </div>
            <div class="col-md-4 d-flex align-items-end">
                <button type="submit" name="test_event" class="btn btn-primary">
                    <i class="fa-solid fa-vial"></i> Test Event
                </button>
            </div>
            <div class="col-md-4 d-flex align-items-end">
                <a href="settings.php" class="btn btn-secondary">
                    <i class="fa-solid fa-cog"></i> Go to Settings
                </a>
            </div>
        </div>
    </form>
    
    <div class="mt-4">
        <h6>Setup Instructions</h6>
        <ol>
            <li>Go to <a href="https://business.facebook.com/events_manager" target="_blank">Facebook Events Manager</a></li>
            <li>Create a new Pixel or use existing</li>
            <li>Copy Pixel ID and paste in <a href="settings.php">Settings</a></li>
            <li>Generate Access Token from Facebook Business Settings</li>
            <li>Enable CAPI for server-side tracking</li>
            <li>Test using the button above</li>
        </ol>
        
        <div class="alert alert-info mt-3">
            <strong>Tip:</strong> After setting up, use the <a href="https://www.facebook.com/events_manager2/list/pixel/<?php echo FACEBOOK_PIXEL_ID; ?>/test_events" target="_blank">Facebook Pixel Test Events</a> tool to verify events are being received.
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>