#!/usr/bin/env python3

import sys
from pathlib import Path
from datetime import datetime, date, timezone
from typing import Optional, NamedTuple
from dataclasses import dataclass
from bs4 import BeautifulSoup
import locale
from calendarstuff import get_events


@dataclass
class EventDetails:
    weekday: str
    date: str
    time: str
    summary: str
    location: str

    def to_html(self) -> str:
        return (
            f'<div class="nextevent">{self.weekday} '
            f'<strong>{self.date}{self.time}, {self.summary}</strong>'
            f'{self.location}</div>'
        )

class EventProcessor:
    def __init__(self, ics_url: str, content_file: str):
        self.ics_url = ics_url
        self.content_file = Path(content_file)
        self.fallback_html = (
            '<div class="nextevent">Leider unbekannt, aber '
            '<strong>frag mal den Vorstand</strong> der müsste es wissen</div>'
        )
    
    @property
    def fallback_content(self) -> str:
        return f"""_model: htmlpage
---
title: Willkommen beim PC Stammertal
---
html:

<h2>Unser nächster Anlass: </h2><br>
{self.fallback_html}
<div>
  &nbsp;<br>
  &nbsp;<br>
  &nbsp;<br>
</div>
<h3>...und auch nicht verpassen:</h3>
<a href="https://www.wyland25.ch/" target="_blank"><img src="/images/zhksf.png" alt="27. Zürcher Kantonalschützenfest 2025" class="stamp"></a>
<div class="threecolumn">
  <div>
    <a href="termine/"><img src=" /images/termine_square.jpg" alt="Terminkalender"> All unsere Termine</a>
  </div>
  <div>
    <a href="about/"><img src="/images/about_square.jpg" alt="Buch"> Alle Infos über uns</a>
  </div>
  <div>
    <a href="kontakt/"><img src="/images/kontakt_square.jpg" alt="Briefe"> Kontaktiere uns</a>
  </div>
</div>
---
_template:

page.html
"""
        
    def setup_locale(self) -> None:
        try:
            locale.setlocale(locale.LC_TIME, 'de_DE.UTF-8')
        except locale.Error as e:
            print(f"Warning: Failed to set locale: {e}", file=sys.stderr)

    def format_event_time(self, start: datetime | date) -> str:
        return "" if isinstance(start, date) and not isinstance(start, datetime) else start.strftime(" %-H:%M")

    def get_next_event(self) -> Optional[EventDetails]:
        try:

            events = get_events(self.ics_url)
            
            event = events[0]
            start = event.get('dtstart').dt

            return EventDetails(
                weekday=start.strftime("%A"),
                date=start.strftime("%-d. %B"),
                time=self.format_event_time(start),
                summary=event.get('summary', ''),
                location=f", {event.get('location')}" if event.get('location') else ""
            )
            
        except (StopIteration, AttributeError) as e:
            print(f"No upcoming events found: {e}", file=sys.stderr)
            return None

    def read_current_content(self) -> tuple[str, str]:
        try:
            content = self.content_file.read_text()
            soup = BeautifulSoup(content, 'html.parser')
            events = soup.find_all('div', {'class': 'nextevent'})
            return content, str(events[0]) if len(events) == 1 else ""
            
        except (IOError, IndexError) as e:
            print(f"Error reading content file: {e}", file=sys.stderr)
            return "", ""

    def process(self) -> str:
        self.setup_locale()
        
        content, source_str = self.read_current_content()
        if not content:
            return self.fallback_content
            
        if len(source_str) > 0 and source_str in content:
            event = self.get_next_event()
            if not event:
                return content.replace(source_str, self.fallback_html).rstrip()
                
            return content.replace(source_str, event.to_html()).rstrip()
            
        return self.fallback_content

def main() -> None:
    if len(sys.argv) != 2:
        print(f"Usage: {sys.argv[0]} <ICS_URL>", file=sys.stderr)
        sys.exit(1)
        
    processor = EventProcessor(
        ics_url=sys.argv[1],
        content_file="/opt/lektor/project/content/contents.lr"
    )
    
    print(processor.process(), end='')
    print("")
    print("")

if __name__ == "__main__":
    main()