from dbmodel import Class
from markdown_pdf import MarkdownPdf, Section
from datetime import datetime
from collections.abc import Sequence
from pathlib import Path

def get_pdf(class_id: int) -> tuple[MarkdownPdf, str]:
    clas = Class.get_by_id(class_id)
      
    # Create PDF 
    pdf = MarkdownPdf(toc_level=2, mode='gfm-like')
    pdf.meta['title'] = clas.name 
    pdf.meta['author'] = 'Learnlytics by @DerGrumpf'
    pdf.meta['producer'] = 'Learnlytics'
    pdf.meta['subject'] = f'Passed List - {clas.name}'
    pdf.meta['keywords'] = f'Passed List,{clas.name}'
    
    css = None
    with open('./learnlytics/pdf/document_style.css') as f:
        css = f.read()

    return (pdf, css)

def get_pdf_header(class_id: int, filter: Sequence[str], author: str, logo_file: Path = None) -> Section:
    text = ''
    # Append Logo
    if logo_file:
        text += f'![logo]({logo_file})\n' 
    
    # Title
    text += f'# Passed List - {Class.get_by_id(class_id).name}\n'
    
    # Filters applied
    text += '#### Study Programms:\n' 
    for n, filter_el in enumerate(filter, start=1):
        text += f'{n}. {filter_el}\n'
    text += '---\n\n'

    # Metadata
    text += f'Author: {author}\n\n'

    date = datetime.now()
    text += f'Created: {date.strftime("%A %d.%m.%Y %H:%M")}\n\n'

    text += 'This document is system-generated and may not require manual signatures.'
    
    return Section(text)
