<?php
/**
 * Production Database Connection Test
 * Upload this to ashof.com.bd root directory and run
 */

error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "<h2>Production Server Database Test</h2>";

// Test 1: Direct connection with correct credentials
echo "<h3>Test 1: Direct Connection</h3>";
$host = 'localhost';
$dbname = 'zashofco_ashof';
$username = 'zashofco_ashof';
$password = '%wfJGwv[hnP3Wcct';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password, [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    ]);
    echo "<p style='color: green; font-size: 18px;'><strong>✓ SUCCESS! Database connected!</strong></p>";
    
    // Check tables
    $stmt = $pdo->query("SHOW TABLES");
    $tables = $stmt->fetchAll(PDO::FETCH_COLUMN);
    echo "<p>Total tables in database: <strong>" . count($tables) . "</strong></p>";
    
} catch(PDOException $e) {
    echo "<p style='color: red; font-size: 18px;'><strong>✗ FAILED!</strong></p>";
    echo "<p>Error: " . $e->getMessage() . "</p>";
}

// Test 2: Check current .env file
echo "<h3>Test 2: Current .env File Check</h3>";
if (file_exists(__DIR__ . '/.env')) {
    echo "<p style='color: green;'>✓ .env file exists</p>";
    $content = file_get_contents(__DIR__ . '/.env');
    
    // Check if it has correct credentials
    if (strpos($content, 'zashofco_ashof') !== false) {
        echo "<p style='color: green;'>✓ Correct database user found</p>";
    } else {
        echo "<p style='color: red;'>✗ WRONG database user in .env file</p>";
        echo "<p>Current .env still has old credentials!</p>";
    }
    
    // Show what's in DB_USER line
    preg_match('/DB_USER=(.+)/', $content, $matches);
    if ($matches) {
        echo "<p>Current DB_USER: <code>" . trim($matches[1]) . "</code></p>";
    }
} else {
    echo "<p style='color: red;'>✗ .env file NOT FOUND!</p>";
}

echo "<hr>";
echo "<h3>Solution:</h3>";
echo "<ol>";
echo "<li>Upload the <strong>.env.final</strong> file</li>";
echo "<li>Rename it to <strong>.env</strong> (overwrite existing)</li>";
echo "<li>Refresh this page - should show green SUCCESS</li>";
echo "<li>Then visit your main page</li>";
echo "</ol>";
