<?php
$active = 'home';
$pageTitle = $project['name'];
require __DIR__ . '/../layout/workspace_header.php';

// ── Determine which KPI cards to show based on project type + user role ──────
$deptName   = strtolower($project['department_name'] ?? '');
$projCode   = strtolower($project['code'] ?? '');
$kpiFormType = strtolower($project['kpi_form_type'] ?? 'none');

// Scheduling KPI — only on scheduling department projects
$showKpiLink = ($deptName === 'scheduling' || $kpiFormType === 'scheduling')
  && (  \App\Models\KpiScheduling::canSubmitEntries($user)
     || ($user['user_type'] === 'manager' && !empty(\App\Models\KpiScheduling::reviewableSubmitters($user['user_id'])))
     || $user['is_super_admin']
     || ($user['user_type'] === 'manager' && ($user['username'] ?? '') === 'sara.lasher'));

$kpiCardIsTeamReview = $showKpiLink && !\App\Models\KpiScheduling::canSubmitEntries($user);
$kpiCardTitle = 'KPI Tracker — Scheduling';
$kpiCardDesc  = $kpiCardIsTeamReview
  ? 'Review, score, and approve your team\'s weekly KPI submissions.'
  : 'Weekly KPI entry, review, and reporting for this project.';
// Reviewers (Cesar, Sara) go straight to review queue — not through index which redirects Cesar away
$kpiCardLink  = $kpiCardIsTeamReview ? '/projects/kpi/review' : '/projects/kpi/my-records';

// Cesar Operations Manager KPI — only on scheduling department projects
$showCesarKpiLink = ($deptName === 'scheduling' || $kpiFormType === 'scheduling')
  && (\App\Models\CesarKpi::canSubmit($user) || \App\Models\CesarKpi::canReview($user));
$cesarCardIsOwnSubmission = \App\Models\CesarKpi::canSubmit($user);
$cesarCardTitle = $cesarCardIsOwnSubmission ? 'My KPI Submission' : 'KPI Tracker — Operations Manager';
$cesarCardLink  = $cesarCardIsOwnSubmission ? '/projects/cesar-kpi/my-records' : '/projects/cesar-kpi/review';

// Accounts KPI — show if user can submit OR review, regardless of department
$showJatKpiLink = \App\Models\AccountsKpi::canSubmit($user) || \App\Models\AccountsKpi::canReview($user);
$jatCardIsOwnSubmission = \App\Models\AccountsKpi::canSubmit($user);
$jatCardTitle = $jatCardIsOwnSubmission ? 'My KPI Submission — Accounts' : 'KPI Tracker — Accounts';
$jatCardDesc  = $jatCardIsOwnSubmission
  ? 'Weekly KPI entry and submission for Accounts.'
  : 'Review and approve Accounts weekly KPI submissions.';
$jatCardLink  = $jatCardIsOwnSubmission ? '/projects/jatinder-kpi/my-records' : '/projects/jatinder-kpi/review';

$anyKpiShowing = $showKpiLink || $showCesarKpiLink || $showJatKpiLink;

// Employee Onboarding — links straight into the real onboarding app.
// Gated only on this project's own code, so no other project's rendering
// is affected in any way.
$showOnboardingLink = ($projCode === 'employee_onboarding');
?>

<div class="topbar">
  <div>
    <h1><?= htmlspecialchars($project['name']) ?></h1>
    <div class="crumbs">
      <a href="/workspace" style="color:var(--blue-600);">Workspace</a> →
      <a href="/department?id=<?= $project['department_id'] ?>" style="color:var(--blue-600);"><?= htmlspecialchars($project['department_name']) ?></a> →
      <?= htmlspecialchars($project['name']) ?>
    </div>
  </div>
  <div class="user-chip"><span class="dot"></span> <?= htmlspecialchars($user['name']) ?></div>
</div>

<?php if ($showKpiLink): ?>
  <div class="card" style="background:linear-gradient(120deg,rgba(79,110,247,0.06),rgba(124,58,224,0.04));">
    <h3>📊 <?= htmlspecialchars($kpiCardTitle) ?></h3>
    <p style="color:var(--text-muted);font-size:13px;margin-bottom:14px;"><?= htmlspecialchars($kpiCardDesc) ?></p>
    <a href="<?= htmlspecialchars($kpiCardLink) ?>" class="btn-primary" style="text-decoration:none;">Open KPI Tracker →</a>
  </div>
<?php endif; ?>

<?php if ($showCesarKpiLink): ?>
  <div class="card" style="background:linear-gradient(120deg,rgba(16,185,129,0.06),rgba(5,150,105,0.04));">
    <h3>📊 <?= htmlspecialchars($cesarCardTitle) ?></h3>
    <p style="color:var(--text-muted);font-size:13px;margin-bottom:14px;">
      <?= $cesarCardIsOwnSubmission ? 'Weekly KPI entry and submission for Operations Manager.' : 'Review and approve Operations Manager weekly KPI submissions.' ?>
    </p>
    <a href="<?= htmlspecialchars($cesarCardLink) ?>" class="btn-primary" style="text-decoration:none;">Open KPI Tracker →</a>
  </div>
<?php endif; ?>

<?php if ($showJatKpiLink): ?>
  <div class="card" style="background:linear-gradient(120deg,rgba(245,158,11,0.06),rgba(217,119,6,0.04));">
    <h3>📊 <?= htmlspecialchars($jatCardTitle) ?></h3>
    <p style="color:var(--text-muted);font-size:13px;margin-bottom:14px;"><?= htmlspecialchars($jatCardDesc) ?></p>
    <a href="<?= htmlspecialchars($jatCardLink) ?>" class="btn-primary" style="text-decoration:none;">Open KPI Tracker →</a>
  </div>
<?php endif; ?>

<?php if ($showOnboardingLink): ?>
  <div class="card" style="background:linear-gradient(120deg,rgba(59,130,246,0.06),rgba(37,99,235,0.04));">
    <h3>🧾 Employee Onboarding</h3>
    <p style="color:var(--text-muted);font-size:13px;margin-bottom:14px;">
      Add new hires and send onboarding links here. Open the portal to add a hire or send Phase A / Phase B.
    </p>
    <a href="/onboarding" class="btn-primary" style="text-decoration:none;">Open Employee Onboarding →</a>
  </div>
  <div class="card" style="padding:0;overflow:hidden;">
    <iframe id="onbEmbedFrame" src="/onboarding/embed" style="width:100%;border:none;display:block;min-height:120px;" title="Employee Onboarding status"></iframe>
  </div>
  <script>
  window.addEventListener('message', function(e){
    if (e.data && e.data.type === 'onb-embed-resize') {
      var f = document.getElementById('onbEmbedFrame');
      if (f) f.style.height = Math.max(e.data.height + 20, 120) + 'px';
    }
  });
  </script>
<?php endif; ?>

<?php if (!$anyKpiShowing && !$showOnboardingLink && empty($widgets)): ?>
  <div class="card">
    <h3>Project Dashboard</h3>
    <p style="color:var(--text-muted);font-size:13px;">
      This project's dashboard hasn't been configured yet. Once forms, reports and KPIs are
      migrated for <strong><?= htmlspecialchars($project['name']) ?></strong>, they'll appear here automatically.
    </p>
  </div>
<?php elseif (!empty($widgets)): ?>
  <div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(220px,1fr));gap:16px;">
    <?php foreach ($widgets as $w): ?>
      <div class="card" style="margin-bottom:0;">
        <h3 style="font-size:13px;color:var(--text-muted);text-transform:uppercase;"><?= htmlspecialchars($w['title']) ?></h3>
        <div style="font-size:22px;font-weight:800;"><?= htmlspecialchars($w['widget_type']) ?></div>
      </div>
    <?php endforeach; ?>
  </div>
<?php endif; ?>

<?php require __DIR__ . '/../layout/admin_footer.php'; ?>
