import re
from pathlib import Path

path = Path(__file__).resolve().parent.parent / 'home' / 'templates' / 'home' / 'index.html'
content = path.read_text(encoding='utf-8')

content = re.sub(
    r'(<div class="cflow-track" id="fc-company-track">).*?(</div>\s*<div class="cflow-nav">)',
    '''<div class="cflow-track" id="fc-company-track">
        {% for company in featured_companies %}
        {% include 'home/partials/company_carousel_card.html' %}
        {% empty %}
        <p style="padding:40px;color:var(--text-muted);">Featured companies coming soon.</p>
        {% endfor %}
      </div>
    <div class="cflow-nav">''',
    content,
    count=1,
    flags=re.DOTALL,
)

podcast_block = '''    <div class="pod-grid">
      {% if featured_podcast %}
      <a href="{{ featured_podcast.get_absolute_url }}" class="pod-ep-card pod-ep-featured reveal" style="cursor:pointer;text-decoration:none;color:inherit;">
        <div class="pod-ep-cover">
          {% if featured_podcast.cover_image_url %}<img loading="lazy" decoding="async" src="{{ featured_podcast.cover_image_url }}" alt="{{ featured_podcast.title }}" class="pod-ep-img" />{% endif %}
          <div class="pod-ep-overlay"></div>
          <div class="pod-play-btn"><i class="fas fa-play"></i></div>
          <span class="pod-ep-num">EP. {{ featured_podcast.episode_number }}</span>
        </div>
        <div class="pod-ep-body">
          <div class="pod-ep-meta"><i class="fas fa-clock"></i> {{ featured_podcast.duration_display }} &nbsp;·&nbsp; <i class="fas fa-calendar"></i> {{ featured_podcast.published_at|date:"M j, Y" }}</div>
          <h3 class="pod-ep-title">{{ featured_podcast.title }}</h3>
          <p class="pod-ep-desc">{{ featured_podcast.description|truncatewords:35 }}</p>
          {% if featured_podcast.guest_name %}<div class="pod-ep-guest"><span>{{ featured_podcast.guest_name }}</span></div>{% endif %}
        </div>
      </a>
      {% endif %}
      <div class="pod-ep-list">
        {% for episode in latest_podcast_episodes %}
        <a href="{{ episode.get_absolute_url }}" class="pod-mini-card reveal" data-delay="{{ forloop.counter0|add:1 }}00" style="cursor:pointer;text-decoration:none;color:inherit;">
          <div class="pod-mini-thumb">
            {% if episode.cover_image_url %}<img loading="lazy" decoding="async" src="{{ episode.cover_image_url }}" alt="Ep {{ episode.episode_number }}" />{% endif %}
            <div class="pod-mini-play"><i class="fas fa-play"></i></div>
          </div>
          <div class="pod-mini-body">
            <span class="pod-ep-num-sm">EP. {{ episode.episode_number }}</span>
            <div class="pod-mini-title">{{ episode.title }}</div>
            <div class="pod-mini-meta"><i class="fas fa-clock"></i> {{ episode.duration_display }} &nbsp;·&nbsp; {{ episode.published_at|date:"M j, Y" }}</div>
          </div>
        </a>
        {% endfor %}
      </div>
    </div>'''

content = re.sub(
    r'    <div class="pod-grid">.*?    </div>\s*\n\s*<div class="text-center" style="margin-top:40px;">\s*\n\s*<a href="{% url \'podcast:list\' %}"',
    podcast_block + '\n\n    <div class="text-center" style="margin-top:40px;">\n      <a href="{% url \'podcast:list\' %}"',
    content,
    count=1,
    flags=re.DOTALL,
)

content = content.replace(
    "Join 150,000+ professionals and 10,000+ companies on Zambia's premier job platform",
    "Join {{ stats.seekers|intcomma }} professionals and {{ stats.companies|intcomma }} companies on Zambia's premier job platform",
)

path.write_text(content, encoding='utf-8')
print('Homepage patched successfully.')
