<?php
if (!isset($task_gid)) return;

$dependencies_url = "https://app.asana.com/api/1.0/tasks/{$task_gid}/dependencies?opt_fields=gid,name,assignee.name,due_on,completed";
$response = wp_remote_get($dependencies_url, [
    'headers' => ['Authorization' => 'Bearer ' . get_option('asana_access_token')]
]);

$data         = json_decode(wp_remote_retrieve_body($response), true);
$dependencies = $data['data'] ?? [];
?>

<section class="bg-white shadow rounded-lg p-6">
  <h2 class="text-lg font-semibold text-gray-700 mb-4">Task Dependencies</h2>

  <?php if (empty($dependencies)): ?>
    <p class="text-sm text-gray-500">This task has no dependencies.</p>
  <?php else: ?>
    <ul class="space-y-3">
      <?php foreach ($dependencies as $dep):
        $gid      = $dep['gid'] ?? '';
        $title    = $dep['name'] ?? '(Untitled)';
        $assignee = $dep['assignee']['name'] ?? 'Unassigned';
        $due      = $dep['due_on'] ?? 'No due date';
        $status   = $dep['completed'] ? '✅ Completed' : '⏳ In Progress';
        $link     = "https://app.asana.com/0/0/{$gid}";
      ?>
      <li class="border border-gray-200 rounded bg-gray-50 hover:bg-gray-100">
        <a href="<?= esc_url($link) ?>" target="_blank" rel="noopener noreferrer" class="block px-4 py-3">
          <div class="font-medium text-blue-600 hover:underline"><?= esc_html($title) ?></div>
          <div class="text-xs text-gray-500">Assigned to: <?= esc_html($assignee) ?></div>
          <div class="text-xs text-gray-500">Due: <?= esc_html($due) ?> — <?= esc_html($status) ?></div>
        </a>
      </li>
      <?php endforeach; ?>
    </ul>
  <?php endif; ?>
</section>
