#!/usr/bin/env python3
"""
Update the valentinnews.sailor.bar site with custom news (Lenta.ru, Habr, AI agents, development, space)
in a lenta.ru style layout.
"""
import os, re, subprocess, sys
from datetime import datetime

# Step 1: Run the custom news aggregator script to generate digest
print("Running custom news aggregator...")
skill_script = '/opt/data/skills/productivity/custom-news-aggregator/scripts/gather_custom_news.py'
result = subprocess.run([sys.executable, skill_script], capture_output=True, text=True)
print(result.stdout)
if result.stderr:
    print("STDERR:", result.stderr)

# Step 2: Read the generated digest
digest_path = os.path.expanduser('~/custom-news-digest.md')
print(f"Reading digest from {digest_path}")
try:
    with open(digest_path, 'r', encoding='utf-8') as f:
        lines = f.readlines()
except FileNotFoundError:
    print("Digest file not found!")
    sys.exit(1)

# Parse sections: each section starts with '## Feed Name'
def parse_sections(lines):
    sections = {}
    current = None
    for line in lines:
        stripped = line.strip()
        if stripped.startswith('## ') and not stripped.startswith('*Сгенерировано:'):
            current = stripped[3:]  # remove '## '
            sections[current] = []
        elif current is not None and line.strip().startswith('- ['):
            # format: - [Title](url) — description || image_url (optional)
            match = re.match(r'\s*-\s*\[([^\]]+)\]\(([^)]+)\)\s*—\s*(.+?)(?:\s*\|\|\s*(.+))?$', line)
            if match:
                title, url, desc, img_url = match.groups()
                sections[current].append({'title': title.strip(), 'url': url.strip(), 'desc': desc.strip(), 'img_url': img_url.strip() if img_url else None})
    return sections

sections = parse_sections(lines)

# Helper to get source domain
def get_source(url):
    try:
        from urllib.parse import urlparse
        return urlparse(url).netloc.replace('www.', '')
    except:
        return 'source'

# Generate HTML cards
def card(item, img_url=None):
    source = get_source(item['url'])
    img = img_url or f'https://source.unsplash.com/random/400x250/?news,{source}'
    return f'''
<div class="card mb-4 shadow-sm">
  <div class="row g-0">
    <div class="col-md-4">
      <img src="{img}" class="img-fluid rounded-start" alt="{item['title']}">
    </div>
    <div class="col-md-8">
      <div class="card-body">
        <h5 class="card-title"><a href="{item['url']}" target="_blank" rel="noopener">{item['title']}</a></h5>
        <p class="card-text">{item['desc']}</p>
        <div class="d-flex justify-content-between align-items-center">
          <small class="text-muted">{source}</small>
          <small class="text-muted">{datetime.now().strftime('%d %b %Y')}</small>
        </div>
      </div>
    </div>
  </div>
</div>
'''

# Define order of sections as we want them to appear
section_order = [
    "Lenta.ru",
    "Habr overall",
    "Habr AI",
    "Habr Development",
    "Habr Space",
    "AI agents (arXiv cs.AI)",
    "Development (GitHub Blog)",
    "Space (NASA Breaking News)",
]

# Build HTML
html = f'''<!DOCTYPE html>
<html lang="ru">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Лента новостей — пользовательская подборка</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <style>
    body {{ background-color: #f8f9fa; }}
    .header {{ background: #0d6efd; color: white; padding: 2rem 0; }}
    .header h1 {{ margin: 0; font-weight: 300; }}
    .header p {{ margin: 0.5rem 0 0; opacity: 0.9; }}
    .section-title {{ font-size: 1.75rem; margin-top: 2rem; margin-bottom: 1rem; color: #212529; border-left: 4px solid #0d6efd; padding-left: 0.5rem; }}
    .footer {{ text-align: center; padding: 2rem 0; color: #6c757d; font-size: 0.9rem; }}
    @media (max-width: 768px) {{
      .card {{ flex-direction: column; }}
      .col-md-4 {{ width: 100%; }}
      .col-md-8 {{ width: 100%; }}
      .img-fluid {{ width: 100%; height: 200px; object-fit: cover; }}
    }}
  </style>
</head>
<body>
  <header class="header text-center">
    <div class="container">
      <h1 class="display-5">Лента новостей</h1>
      <p class="lead">Подборка: Lenta.ru, Habr, AI-агенты, разработка, космос</p>
    </div>
  </header>

  <div class="container py-4">
    <div class="row">
      <div class="col-lg-8">
'''
# Add each section
for sect in section_order:
    if sect in sections and sections[sect]:
        html += f'        <h2 class="section-title">{sect}</h2>\n'
        html += ''.join([card(i) for i in sections[sect][:10]])  # limit to 10 per section
        html += '\n'

html += '''      </div>
      <div class="col-lg-4">
        <div class="sticky-top bg-light p-3 rounded">
          <h5>О проекте</h5>
          <p class="small">Автоматически собранный дайджест из выбранных источников. Обновляется каждые 3 дня.</p>
          <hr>
          <h6>Источники</h6>
          <ul class="list-unstyled small">
'''
# List unique sources from all sections
all_sources = set()
for sect, items in sections.items():
    for i in items:
        all_sources.add(get_source(i['url']))
for src in sorted(all_sources):
    html += f'            <li>{src}</li>\n'
html += '''          </ul>
        </div>
      </div>
    </div>
  </div>

  <footer class="footer">
    <div class="container">
      <p>Принцип чтения: для каждой темы минимум 2 источника с разных сторон.</p>
      <p><a href="#" class="text-decoration-none">Наверх</a></p>
    </div>
  </footer>

  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
'''

# Write file
repo_dir = '/opt/hermes/valentinnews.sailor.bar'
output_path = os.path.join(repo_dir, 'index.html')
print(f"Writing to {output_path}")
with open(output_path, 'w', encoding='utf-8') as f:
    f.write(html)

# Commit and push
os.chdir(repo_dir)
subprocess.run(['git', 'add', 'index.html'], check=True)
subprocess.run(['git', 'commit', '-m', 'Update site with custom news digest'], check=True)
subprocess.run(['git', 'push', 'origin', 'master'], check=True)

print('Site updated successfully.')