<?php
// Test database connection directly
$host = 'localhost';
$username = 'kongutourist_offers';
$password = 'Uq$yrK527t(d.@7[';
$database = 'kongutourist_offers';

header('Content-Type: application/json');

try {
    $conn = new mysqli($host, $username, $password, $database);
    
    if ($conn->connect_error) {
        echo json_encode([
            'success' => false, 
            'message' => 'Connection failed: ' . $conn->connect_error
        ]);
        exit;
    }
    
    // Test if offers table exists
    $result = $conn->query("SHOW TABLES LIKE 'offers'");
    if ($result->num_rows === 0) {
        echo json_encode([
            'success' => false, 
            'message' => 'Offers table does not exist'
        ]);
        exit;
    }
    
    // Test inserting a record
    $startDate = date('Y-m-d H:i:s');
    $endDate = date('Y-m-d H:i:s', strtotime('+7 days'));
    
    $sql = "INSERT INTO offers (title, image_url, start_date, end_date) VALUES (?, ?, ?, ?)";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ssss", 'Test Offer', 'images/offers/test.jpg', $startDate, $endDate);
    
    if ($stmt->execute()) {
        echo json_encode([
            'success' => true, 
            'message' => 'Database connection and insert test successful'
        ]);
    } else {
        echo json_encode([
            'success' => false, 
            'message' => 'Insert failed: ' . $stmt->error
        ]);
    }
    
    $stmt->close();
    $conn->close();
    
} catch (Exception $e) {
    echo json_encode([
        'success' => false, 
        'message' => 'Exception: ' . $e->getMessage()
    ]);
}
?>
