<?php
require_once __DIR__ . '/../../app/helpers/functions.php';
require_once __DIR__ . '/../../app/helpers/csrf.php';
require_once __DIR__ . '/../../app/middleware/auth.php';

session_boot();
require_login();

$conn = require __DIR__ . '/../../app/config/database.php';
$u = current_user();

/* ===============================
   PROFILE PHOTO UPLOAD
================================ */
if (is_post() && isset($_FILES['photo'])) {
    csrf_verify();

    $f = $_FILES['photo'];

    if ($f['error'] === 0) {
        $mime = mime_content_type($f['tmp_name']);
        if (!in_array($mime, ['image/jpeg','image/png'])) {
            flash('msg','Only JPG or PNG allowed.');
            redirect('profile_photo.php');
        }

        $ext  = $mime === 'image/png' ? 'png' : 'jpg';
        $name = 'profile_'.$u['id'].'_'.time().'.'.$ext;
        $path = '/uploads/profiles/'.$name;

        move_uploaded_file(
            $f['tmp_name'],
            __DIR__ . '/../../public' . $path
        );

        $st = $conn->prepare("UPDATE users SET photo=? WHERE id=?");
        $st->bind_param("si", $path, $u['id']);
        $st->execute();

        // refresh session photo
        $_SESSION['user']['photo'] = $path;

        flash('msg','Profile photo updated.');
        redirect('profile.php');
    }
}

/* ===============================
   PROFILE COMPLETION
================================ */
$completionChecks = [
    !empty($u['full_name']),
    !empty($u['phone']),
    !empty($u['photo'] ?? null),
    ($u['kyc_status'] ?? '') === 'verified'
];
$profileCompletion = (int)(array_sum($completionChecks) / count($completionChecks) * 100);

$title = 'Profile — Astrabank';
include __DIR__ . '/../../views/partials/head.php';
include __DIR__ . '/../../views/partials/customer_sidebar.php';

$msg = flash('msg');
?>

<!-- PROFILE PHOTO -->
<div class="d-flex align-items-center gap-3 mb-4">
  <img
    src="<?= e($u['photo'] ?? '/uploads/profile/avatar.png') ?>"
    alt="Profile photo"
    style="width:90px;height:90px;object-fit:cover"
    class="rounded-circle border"
  >

  <div>
    <div class="fw-bold"><?= e($u['full_name'] ?? '') ?></div>
    <a href="profile_photo.php" class="small text-decoration-none">
      Change profile photo
    </a>
  </div>
</div>

<main class="flex-grow-1" style="margin-left:240px; min-height:100vh;">
<div class="container-fluid py-4">

  <?php if ($msg): ?>
    <div class="alert alert-info ab-card"><?= e($msg) ?></div>
  <?php endif; ?>

  <div class="card ab-card p-4" style="max-width:800px">

    <h5 class="fw-bold mb-3">Profile</h5>

    <!-- PROFILE SUMMARY -->
    <div class="row g-3 mb-3">
      <div class="col-md-6">
        <div class="ab-muted">Email</div>
        <div class="fw-semibold"><?= e($u['email'] ?? '') ?></div>
      </div>
      <div class="col-md-6">
        <div class="ab-muted">KYC Status</div>
        <div class="fw-semibold text-capitalize">
          <?= e($u['kyc_status'] ?? 'pending') ?>
        </div>
      </div>
    </div>

    <!-- PROFILE COMPLETION -->
    <div class="mb-4">
      <div class="ab-muted mb-1">Profile completion</div>
      <div class="progress">
        <div
          class="progress-bar <?= $profileCompletion < 100 ? 'bg-warning' : 'bg-success' ?>"
          style="width:<?= $profileCompletion ?>%">
          <?= $profileCompletion ?>%
        </div>
      </div>
    </div>

    <hr>

    <!-- PROFILE FORM -->
    <form method="post" class="row g-3 mb-4">
      <?= csrf_field() ?>

      <div class="col-md-6">
        <label class="form-label">Full Name</label>
        <input
          name="full_name"
          class="form-control"
          value="<?= e($u['full_name'] ?? '') ?>"
          required
        >
      </div>

      <div class="col-md-6">
        <label class="form-label">Phone</label>
        <input
          name="phone"
          class="form-control"
          value="<?= e($u['phone'] ?? '') ?>"
          placeholder="Optional"
        >
      </div>

      <div class="col-12">
        <button class="btn ab-btn text-white">Save</button>
      </div>
    </form>

    <hr>

    <!-- SECURITY ACTIONS (NEW) -->
    <div class="row g-3">
      <div class="col-md-6">
        <a href="change_password.php" class="btn btn-outline-primary w-100">
          🔒 Change Password
        </a>
      </div>

      <div class="col-md-6">
        <a href="change_pin.php" class="btn btn-outline-warning w-100">
          🔑 Change PIN
        </a>
      </div>
    </div>

  </div>
</div>
</main>

<?php include __DIR__ . '/../../views/partials/foot.php'; ?>
