<?php
require_once 'db.php';
require_once 'auth_check.php';
require_once 'currency.php';

$active_plan = $conn->query("
    SELECT p.*, up.expires_at, up.joined_at
    FROM user_plans up
    JOIN plans p ON p.id = up.plan_id
    WHERE up.user_id = $user_id AND up.active = 1
      AND (up.expires_at IS NULL OR up.expires_at > NOW())
    ORDER BY p.sort_order DESC LIMIT 1
")->fetch_assoc();

$task_plans = $conn->query("
    SELECT * FROM plans WHERE active=1 ORDER BY sort_order ASC
")->fetch_all(MYSQLI_ASSOC);

$tables_ok = $conn->query("SHOW TABLES LIKE 'investment_plans'")->num_rows > 0;
$investment_plans = [];
if ($tables_ok) {
    $investment_plans = $conn->query("
        SELECT * FROM investment_plans WHERE active=1 ORDER BY sort_order ASC, id ASC
    ")->fetch_all(MYSQLI_ASSOC);
}

function task_income($plan) {
    $daily   = (float)$plan['daily_earnings'];
    $monthly = $daily * 30;
    $yearly  = $daily * 365;
    return [$daily, $monthly, $yearly];
}

function invest_income($plan) {
    $min = (float)$plan['min_amount'];
    $max = (float)$plan['max_amount'];
    $mid = ($min + $max) / 2;
    $daily   = $mid * ($plan['daily_rate'] / 100);
    $monthly = $daily * 30;
    $yearly  = $daily * 365;
    return [$daily, $monthly, $yearly];
}

function fmt($n) {
    return number_format((float)$n, 2);
}

$plan_colors = ['#3b8ef5','#9b72f5','#00d4aa','#f0c040','#ff5b7a','#00c6ff','#e84393'];
?>
<!DOCTYPE html>
<html lang="sw">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<title>Income Summary</title>
<style>
*{margin:0;padding:0;box-sizing:border-box}
html,body{
  font-family:'Times New Roman',Times,serif;
  background:#c8e6c9;
  min-height:100vh;
  padding:8px 6px 70px;
}

/* TOPBAR */
.topbar{
  background:#2e7d32;
  border-radius:8px 8px 0 0;
  padding:7px 10px;
  display:flex;align-items:center;justify-content:space-between;
}
.back-btn{
  color:#fff;font-size:14px;text-decoration:none;
  display:flex;align-items:center;justify-content:center;
  width:26px;height:26px;border-radius:50%;
  background:rgba(255,255,255,.15);border:1px solid rgba(255,255,255,.3);
}
.topbar-title{color:#fff;font-size:12px;font-weight:700}
.topbar-badge{
  font-size:9px;font-weight:700;
  background:rgba(255,255,255,.15);color:#fff;
  border:1px solid rgba(255,255,255,.3);
  border-radius:20px;padding:2px 8px;
}

/* TABLE */
table{
  width:100%;border-collapse:collapse;
  background:#fff;
  font-family:'Times New Roman',Times,serif;
  border-radius:0 0 8px 8px;
  overflow:hidden;
  table-layout:fixed;
}

.sec-label{
  padding:4px 8px;
  font-size:9px;font-weight:700;letter-spacing:.6px;
  color:#fff;text-align:left;
}
.sec-label.task-hd{background:#2e7d32}
.sec-label.inv-hd{background:#512da8}

thead.task-head tr{background:#388e3c}
thead.inv-head tr{background:#4527a0}

th{
  color:#fff;padding:4px 3px;
  text-align:center;font-size:8.5px;font-weight:700;
  white-space:nowrap;overflow:hidden;
}
th:first-child{text-align:left;padding-left:5px}

tbody tr{border-bottom:1px solid #e8f5e9}
tbody tr:last-child{border-bottom:2px solid #c8e6c9}
tbody tr:nth-child(even){background:#f9fbe7}
tbody tr.active-row{background:#e0f7e9}

td{
  padding:4px 3px;
  vertical-align:middle;
  text-align:center;
  font-size:9px;
  overflow:hidden;
}
td:first-child{text-align:left;padding-left:5px}

/* PLAN NAME */
.dot{display:inline-block;width:7px;height:7px;border-radius:50%;flex-shrink:0;vertical-align:middle;margin-right:3px}
.pname{font-weight:700;font-size:9px;color:#1b5e20;display:block;line-height:1.3}
.psub{font-size:8px;color:#558b2f;display:block;line-height:1.2}
.active-pill{
  display:inline-block;padding:1px 4px;border-radius:3px;
  font-size:7.5px;font-weight:700;
  background:#e8f5e9;color:#2e7d32;border:1px solid #81c784;
}

/* VALUE COLORS */
.v-tasks{color:#1565c0;font-weight:700;font-size:9px}
.v-daily{color:#212121;font-weight:600;font-size:9px}
.v-monthly{color:#00695c;font-weight:700;font-size:9px}
.v-yearly{color:#e65100;font-weight:700;font-size:9px}
.v-rate{color:#bf360c;font-weight:700;font-size:9px}
.v-cycle{color:#4527a0;font-weight:700;font-size:9px}
.v-return{color:#c62828;font-weight:700;font-size:8px}
.v-swatch{display:inline-block;width:8px;height:8px;border-radius:2px;vertical-align:middle;margin-right:3px}
.lbl{color:#999;font-size:7px;display:block;line-height:1.1}
</style>
</head>
<body>

<div class="topbar">
  <a href="dashboard.php" class="back-btn">&#8592;</a>
  <div class="topbar-title">Income Summary</div>
  <div class="topbar-badge"><?= $active_plan ? htmlspecialchars($active_plan['name']) : 'No Plan' ?></div>
</div>

<table>

  <!-- VIDEO TASK PLANS -->
  <tbody>
    <tr><td colspan="5" class="sec-label task-hd">VIDEO TASK PLANS</td></tr>
  </tbody>
  <thead class="task-head">
    <tr>
      <th style="width:32%">Plan</th>
      <th style="width:13%">Tasks</th>
      <th style="width:18%">Daily</th>
      <th style="width:18%">Monthly</th>
      <th style="width:19%">Yearly</th>
    </tr>
  </thead>
  <tbody>
    <?php if (empty($task_plans)): ?>
    <tr><td colspan="5" style="text-align:center;color:#888;padding:8px;font-size:10px">No task plans.</td></tr>
    <?php else: ?>
    <?php foreach ($task_plans as $i => $pl):
      [$daily,$monthly,$yearly] = task_income($pl);
      $is_current = $active_plan && $active_plan['id'] == $pl['id'];
      $dot_color  = !empty($pl['color_from']) ? $pl['color_from'] : $plan_colors[$i % count($plan_colors)];
      $bg_style   = !empty($pl['color_from']) && !empty($pl['color_to'])
                    ? "background:linear-gradient(135deg,{$pl['color_from']},{$pl['color_to']})"
                    : "background:{$dot_color}";
    ?>
    <tr class="<?= $is_current ? 'active-row' : '' ?>">
      <td>
        <span class="dot" style="<?= $bg_style ?>"></span>
        <span class="pname">
          <?= htmlspecialchars($pl['name']) ?>
          <?php if ($is_current): ?><span class="active-pill">&#10003;</span><?php endif; ?>
        </span>
        <span class="psub">
          <?= number_format($pl['price'],0) ?> <?= $currency ?>
          <?php if ($pl['duration_days'] > 0): ?>&middot;<?= $pl['duration_days'] ?>d<?php endif; ?>
        </span>
      </td>
      <td><span class="v-tasks"><?= $pl['daily_tasks'] ?>x</span></td>
      <td><span class="v-daily"><?= fmt($daily) ?></span></td>
      <td><span class="v-monthly"><?= fmt($monthly) ?></span></td>
      <td><span class="v-yearly"><?= fmt($yearly) ?></span></td>
    </tr>
    <?php endforeach; ?>
    <?php endif; ?>
  </tbody>

  <!-- WEALTH FUND PLANS -->
  <tbody>
    <tr><td colspan="5" class="sec-label inv-hd">WEALTH FUND PLANS</td></tr>
  </tbody>
  <thead class="inv-head">
    <tr>
      <th style="width:32%">Plan</th>
      <th style="width:13%">Rate</th>
      <th style="width:18%">Daily</th>
      <th style="width:18%">Monthly</th>
      <th style="width:19%">Yearly</th>
    </tr>
  </thead>
  <tbody>
    <?php if (!$tables_ok): ?>
    <tr><td colspan="5" style="text-align:center;color:#888;padding:8px;font-size:10px">Run investment_migration.sql.</td></tr>
    <?php elseif (empty($investment_plans)): ?>
    <tr><td colspan="5" style="text-align:center;color:#888;padding:8px;font-size:10px">No investment plans.</td></tr>
    <?php else: ?>
    <?php foreach ($investment_plans as $p):
      [$daily,$monthly,$yearly] = invest_income($p);
      $total_pct = round($p['daily_rate'] * $p['cycle_days'], 1);
    ?>
    <tr>
      <td>
        <span class="v-swatch" style="background:linear-gradient(135deg,<?= htmlspecialchars($p['color_from']) ?>,<?= htmlspecialchars($p['color_to']) ?>)"></span>
        <span class="pname"><?= htmlspecialchars($p['name']) ?></span>
        <span class="psub">
          <?= number_format($p['min_amount'],0) ?>–<?= number_format($p['max_amount'],0) ?> &middot; <?= $p['cycle_days'] ?>d
          <span class="v-return">(<?= $total_pct ?>%)</span>
        </span>
      </td>
      <td><span class="v-rate"><?= $p['daily_rate'] ?>%</span></td>
      <td><span class="v-daily"><?= fmt($daily) ?></span></td>
      <td><span class="v-monthly"><?= fmt($monthly) ?></span></td>
      <td><span class="v-yearly"><?= fmt($yearly) ?></span></td>
    </tr>
    <?php endforeach; ?>
    <?php endif; ?>
  </tbody>

</table>

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

</body>
</html>