<?php
require_once __DIR__ . '/config/config.php';
require_once __DIR__ . '/includes/functions.php';
$pageTitle = 'Careers';
$currentPage = 'careers';
$jobs = Database::fetchAll("SELECT * FROM careers WHERE is_active=1 ORDER BY sort_order, created_at DESC");
$intro = setting('careers_intro');
include __DIR__ . '/includes/header.php';
?>

<section class="pt-28 pb-16 bg-navy-900">
  <div class="max-w-5xl mx-auto px-4">
    <div class="text-center mb-12">
      <div class="inline-flex items-center gap-2 bg-orange-600/10 border border-orange-600/30 rounded-full px-4 py-1.5 text-xs font-medium text-orange-400 uppercase tracking-wider mb-4">
        Join Our Team
      </div>
      <h1 class="text-4xl md:text-5xl font-black font-display mb-4">Work at <span class="text-orange-500">Rig N Dig</span></h1>
      <?php if ($intro): ?>
      <p class="text-gray-400 max-w-2xl mx-auto"><?= e($intro) ?></p>
      <?php endif; ?>
    </div>

    <!-- Why Work With Us -->
    <div class="grid grid-cols-1 sm:grid-cols-3 gap-5 mb-12">
      <?php foreach ([
        ['🌍', 'Global Exposure', 'Work with manufacturers, distributors, and E&P operators across 4 continents'],
        ['🔧', 'Technical Growth', 'Develop expertise in API standards, industrial equipment, and international sourcing'],
        ['🤝', 'Team Culture', 'Close-knit, professional team where your contribution makes a direct impact'],
      ] as [$ic, $t, $d]): ?>
      <div class="card-dark p-5 text-center">
        <div class="text-3xl mb-3"><?= $ic ?></div>
        <h3 class="font-bold text-white text-sm mb-1"><?= $t ?></h3>
        <p class="text-xs text-gray-400"><?= $d ?></p>
      </div>
      <?php endforeach; ?>
    </div>

    <!-- Job Listings -->
    <?php if ($jobs): ?>
    <div class="space-y-5 mb-12">
      <?php foreach ($jobs as $job): ?>
      <div class="card-dark overflow-hidden hover:border-orange-600/50 transition-colors">
        <div class="p-6">
          <div class="flex flex-wrap items-start justify-between gap-4 mb-3">
            <div>
              <h3 class="text-lg font-bold text-white mb-1"><?= e($job['title']) ?></h3>
              <div class="flex flex-wrap gap-2 text-xs">
                <?php if ($job['department']): ?>
                <span class="bg-navy-600 text-gray-300 px-2.5 py-1 rounded-full"><?= e($job['department']) ?></span>
                <?php endif; ?>
                <span class="bg-orange-600/10 text-orange-400 px-2.5 py-1 rounded-full"><?= ucfirst(str_replace('-', ' ', $job['type'])) ?></span>
                <span class="bg-navy-600 text-gray-300 px-2.5 py-1 rounded-full">📍 <?= e($job['location']) ?></span>
                <?php if ($job['experience']): ?>
                <span class="bg-navy-600 text-gray-300 px-2.5 py-1 rounded-full">⏱️ <?= e($job['experience']) ?></span>
                <?php endif; ?>
              </div>
            </div>
            <?php if ($job['salary_range']): ?>
            <div class="text-right">
              <div class="text-xs text-gray-500">Salary Range</div>
              <div class="text-sm font-semibold text-orange-400"><?= e($job['salary_range']) ?></div>
            </div>
            <?php endif; ?>
          </div>

          <?php if ($job['description']): ?>
          <p class="text-sm text-gray-400 leading-relaxed mb-4"><?= nl2br(e($job['description'])) ?></p>
          <?php endif; ?>

          <?php if ($job['requirements']): ?>
          <div class="mb-4">
            <h4 class="text-xs font-semibold text-orange-400 uppercase tracking-wider mb-2">Requirements</h4>
            <div class="text-sm text-gray-400 whitespace-pre-line"><?= e($job['requirements']) ?></div>
          </div>
          <?php endif; ?>

          <?php if ($job['benefits']): ?>
          <div class="mb-4">
            <h4 class="text-xs font-semibold text-orange-400 uppercase tracking-wider mb-2">What We Offer</h4>
            <div class="text-sm text-gray-400 whitespace-pre-line"><?= e($job['benefits']) ?></div>
          </div>
          <?php endif; ?>

          <a href="mailto:<?= e(setting('email')) ?>?subject=Application: <?= urlencode($job['title']) ?>"
            class="btn-primary text-sm px-6 py-2.5 mt-2 inline-block">
            Apply for This Role
          </a>
        </div>
      </div>
      <?php endforeach; ?>
    </div>
    <?php else: ?>
    <div class="card-dark p-12 text-center mb-8">
      <div class="text-5xl mb-4">🔎</div>
      <h3 class="text-xl font-bold mb-3">No Open Positions Right Now</h3>
      <p class="text-gray-400 mb-6">We're not actively hiring at the moment, but we're always open to hearing from exceptional engineers, technical sales professionals, and workshop technicians.</p>
      <a href="mailto:<?= e(setting('email')) ?>?subject=Speculative Application — Rig N Dig"
        class="btn-primary px-8 py-3">Send a Speculative Application</a>
    </div>
    <?php endif; ?>

    <!-- Open application CTA -->
    <div class="card-dark p-6 bg-navy-800 border-orange-600/30 flex flex-col sm:flex-row items-center gap-4">
      <div class="flex-1">
        <h3 class="font-bold text-white mb-1">Don't see a suitable role?</h3>
        <p class="text-sm text-gray-400">Email us your CV and we'll keep your profile on file for when suitable opportunities arise.</p>
      </div>
      <a href="mailto:<?= e(setting('email')) ?>?subject=Career Enquiry" class="btn-outline whitespace-nowrap px-6 py-2.5 text-sm">Email Your CV</a>
    </div>
  </div>
</section>

<?php include __DIR__ . '/includes/footer.php'; ?>
