<?php
// ============================================================
// PAKCLUB INSTALL.PHP  —  Safe to re-run (IF NOT EXISTS)
// Run once at: yourdomain.com/install.php
// ============================================================
require_once __DIR__ . '/config.php';

$steps = []; $errors = 0;
function run($pdo, $label, $sql) {
    global $steps, $errors;
    try { $pdo->exec($sql); $steps[] = ['ok', $label, '']; }
    catch (PDOException $e) { $steps[] = ['err', $label, $e->getMessage()]; $errors++; }
}

// 1. Add missing columns to users
run($pdo,'users → total_withdrawn','ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `total_withdrawn` DECIMAL(15,2) NOT NULL DEFAULT 0.00');
run($pdo,'users → last_checkin_date','ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `last_checkin_date` DATE DEFAULT NULL');
run($pdo,'users → checkin_streak','ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `checkin_streak` INT DEFAULT 0');

// 2. deposits
run($pdo,'Create `deposits`',"
CREATE TABLE IF NOT EXISTS `deposits` (
  `id`            INT(11) NOT NULL AUTO_INCREMENT,
  `user_id`       INT(11) NOT NULL,
  `order_id`      VARCHAR(64) NOT NULL,
  `amount`        DECIMAL(10,2) NOT NULL,
  `method`        VARCHAR(20) NOT NULL DEFAULT 'EASYPAISA',
  `status`        ENUM('pending','approved','rejected') NOT NULL DEFAULT 'pending',
  `reject_reason` TEXT DEFAULT NULL,
  `apply_wager`   TINYINT(1) NOT NULL DEFAULT 0,
  `created_at`    TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at`    TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `order_id` (`order_id`),
  KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");

// 3. withdrawals
run($pdo,'Create `withdrawals`',"
CREATE TABLE IF NOT EXISTS `withdrawals` (
  `id`              INT(11) NOT NULL AUTO_INCREMENT,
  `user_id`         INT(11) NOT NULL,
  `amount`          DECIMAL(10,2) NOT NULL,
  `method`          VARCHAR(20) NOT NULL DEFAULT 'EASYPAISA',
  `account_name`    VARCHAR(100) NOT NULL,
  `account_number`  VARCHAR(50) NOT NULL,
  `status`          ENUM('pending','approved','rejected') NOT NULL DEFAULT 'pending',
  `payout_order_id` VARCHAR(64) DEFAULT NULL,
  `payout_status`   ENUM('none','processing','sent','failed') NOT NULL DEFAULT 'none',
  `reject_reason`   TEXT DEFAULT NULL,
  `created_at`      TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at`      TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");

// 4. withdrawal_accounts
run($pdo,'Create `withdrawal_accounts`',"
CREATE TABLE IF NOT EXISTS `withdrawal_accounts` (
  `id`             INT(11) NOT NULL AUTO_INCREMENT,
  `user_id`        INT(11) NOT NULL,
  `method`         VARCHAR(20) NOT NULL,
  `account_name`   VARCHAR(100) NOT NULL,
  `account_number` VARCHAR(50) NOT NULL,
  `created_at`     TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `user_method` (`user_id`,`method`),
  KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");

// 5. referral_commissions
run($pdo,'Create `referral_commissions`',"
CREATE TABLE IF NOT EXISTS `referral_commissions` (
  `id`              INT(11) NOT NULL AUTO_INCREMENT,
  `user_id`         INT(11) NOT NULL,
  `from_user_id`    INT(11) NOT NULL,
  `deposit_id`      INT(11) DEFAULT NULL,
  `amount`          DECIMAL(10,2) NOT NULL,
  `level`           TINYINT(1) NOT NULL DEFAULT 1,
  `is_first_deposit` TINYINT(1) NOT NULL DEFAULT 0,
  `created_at`      TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`),
  KEY `from_user_id` (`from_user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
?>
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>PakClub Install</title>
<style>
*{box-sizing:border-box;margin:0;padding:0}
body{background:#0f1117;color:#e8eaf0;font-family:'Segoe UI',sans-serif;padding:40px 20px;min-height:100vh}
.wrap{max-width:760px;margin:0 auto}
h1{color:#f26d0f;font-size:28px;margin-bottom:8px}
p.sub{color:#8892a4;margin-bottom:30px}
.step{display:flex;align-items:flex-start;gap:14px;padding:14px 18px;border-radius:10px;margin-bottom:10px;border:1px solid}
.step.ok{background:rgba(34,197,94,.08);border-color:rgba(34,197,94,.25)}
.step.err{background:rgba(239,68,68,.08);border-color:rgba(239,68,68,.25)}
.badge{padding:3px 12px;border-radius:20px;font-size:11px;font-weight:700;flex-shrink:0;margin-top:2px}
.badge.ok{background:#22c55e;color:#000}
.badge.err{background:#ef4444;color:#fff}
.label{font-weight:600;font-size:15px}
.err-msg{font-size:12px;color:#f87171;margin-top:4px}
.summary{margin-top:30px;padding:20px 24px;border-radius:12px;font-size:16px;font-weight:600}
.summary.done{background:rgba(34,197,94,.12);border:1px solid rgba(34,197,94,.3);color:#4ade80}
.summary.fail{background:rgba(239,68,68,.12);border:1px solid rgba(239,68,68,.3);color:#f87171}
.warn{margin-top:20px;padding:16px 20px;background:rgba(245,158,11,.1);border:1px solid rgba(245,158,11,.3);border-radius:10px;color:#fbbf24;font-size:14px}
</style></head><body><div class="wrap">
<h1>🔧 PakClub — Database Install</h1>
<p class="sub">Running migrations… safe to re-run at any time.</p>
<?php foreach($steps as $s): ?>
<div class="step <?= $s[0] ?>">
  <span class="badge <?= $s[0] ?>"><?= strtoupper($s[0]) ?></span>
  <div><div class="label"><?= htmlspecialchars($s[1]) ?></div>
  <?php if($s[2]): ?><div class="err-msg"><?= htmlspecialchars($s[2]) ?></div><?php endif; ?>
  </div>
</div>
<?php endforeach; ?>
<div class="summary <?= $errors===0?'done':'fail' ?>">
  <?= $errors===0 ? '✅ All migrations completed successfully!' : "⚠️ {$errors} migration(s) failed — check messages above." ?>
</div>
<div class="warn">⚠️ <strong>Delete or password-protect this file</strong> after running it on your live server.</div>
</div></body></html>
