<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'max_power');

define('ADMIN_USER', 'admin');
define('ADMIN_PASS', 'admin123');

$message = '';

try {
    $pdoTest = new PDO("mysql:host=" . DB_HOST, DB_USER, DB_PASS);
    $pdoTest->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $pdoTest->exec("CREATE DATABASE IF NOT EXISTS `" . DB_NAME . "` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_turkish_ci");
    $message .= '<div class="alert alert-success">✓ Veritabanı oluşturuldu (zaten varsa bağlandı).</div>';
    
    $pdo = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=utf8mb4", DB_USER, DB_PASS);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $sql = "CREATE TABLE IF NOT EXISTS orders (
        id INT AUTO_INCREMENT PRIMARY KEY,
        order_no VARCHAR(20) NOT NULL UNIQUE,
        product_id INT NOT NULL,
        product_name VARCHAR(255) NOT NULL,
        product_price DECIMAL(10,2) NOT NULL,
        customer_name VARCHAR(255) NOT NULL,
        customer_phone VARCHAR(30) NOT NULL,
        customer_city VARCHAR(100) NOT NULL,
        customer_district VARCHAR(100) NOT NULL,
        customer_address TEXT NOT NULL,
        payment_type VARCHAR(50) NOT NULL,
        status VARCHAR(20) NOT NULL DEFAULT 'pending',
        cargo_no VARCHAR(100) DEFAULT NULL,
        notes TEXT DEFAULT NULL,
        created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
        updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        INDEX idx_status (status),
        INDEX idx_created_at (created_at)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_turkish_ci";
    
    $pdo->exec($sql);
    $message .= '<div class="alert alert-success">✓ Siparişler tablosu oluşturuldu (zaten varsa güncellendi).</div>';
    
    $checkAdmin = $pdo->query("SHOW TABLES LIKE 'admin_users'");
    if ($checkAdmin->rowCount() == 0) {
        $sql2 = "CREATE TABLE IF NOT EXISTS admin_users (
            id INT AUTO_INCREMENT PRIMARY KEY,
            username VARCHAR(50) NOT NULL UNIQUE,
            password VARCHAR(255) NOT NULL,
            created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_turkish_ci";
        $pdo->exec($sql2);
        
        $hashedPass = password_hash(ADMIN_PASS, PASSWORD_DEFAULT);
        $stmt = $pdo->prepare("INSERT INTO admin_users (username, password) VALUES (?, ?)");
        $stmt->execute([ADMIN_USER, $hashedPass]);
        $message .= '<div class="alert alert-success">✓ Admin kullanıcısı oluşturuldu.</div>';
    } else {
        $message .= '<div class="alert alert-info">ℹ Admin tablosu zaten mevcut.</div>';
    }
    
    $checkPixels = $pdo->query("SHOW TABLES LIKE 'ad_pixels'");
    if ($checkPixels->rowCount() == 0) {
        $sql3 = "CREATE TABLE IF NOT EXISTS ad_pixels (
            id INT AUTO_INCREMENT PRIMARY KEY,
            name VARCHAR(255) NOT NULL,
            platform VARCHAR(100) DEFAULT NULL,
            pixel_code TEXT NOT NULL,
            placement VARCHAR(20) NOT NULL DEFAULT 'all' COMMENT 'all, home, success',
            position VARCHAR(20) NOT NULL DEFAULT 'head' COMMENT 'head, body-top, body-bottom',
            status TINYINT(1) NOT NULL DEFAULT 1,
            sort_order INT DEFAULT 0,
            created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
            updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_turkish_ci";
        $pdo->exec($sql3);
        $message .= '<div class="alert alert-success">✓ Reklam piksel tablosu oluşturuldu.</div>';
    } else {
        $message .= '<div class="alert alert-info">ℹ Reklam piksel tablosu zaten mevcut.</div>';
        try {
            $pdo->exec("ALTER TABLE ad_pixels ADD COLUMN IF NOT EXISTS position VARCHAR(20) NOT NULL DEFAULT 'head' COMMENT 'head, body-top, body-bottom' AFTER placement");
            $message .= '<div class="alert alert-success">✓ Position kolonu güncellendi.</div>';
        } catch(Exception $e) {}
    }
    
    $message .= '<div class="alert alert-success"><strong>Kurulum Tamamlandı!</strong><br>
    Admin Giriş Bilgileri:<br>
    Kullanıcı Adı: <strong>admin</strong><br>
    Şifre: <strong>admin123</strong><br><br>
    <a href="admin/" class="btn btn-primary">Admin Paneline Git</a>
    <hr><strong style="color:#dc3545;">GÜVENLİK İÇİN: Bu install.php dosyasını sunucudan silmeyi unutmayın!</strong></div>';
    
} catch(PDOException $e) {
    $message = '<div class="alert alert-danger">Hata: ' . $e->getMessage() . '</div>';
}
?>
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<title>Kurulum - Max Power Sipariş Sistemi</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">
<div class="container py-5">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card shadow">
                <div class="card-header bg-primary text-white">
                    <h3 class="mb-0">Max Power Sipariş Sistemi Kurulumu</h3>
                </div>
                <div class="card-body">
                    <?php echo $message; ?>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>
