"""Export room list to a styled Excel workbook."""
from openpyxl import Workbook
from openpyxl.styles import Alignment, Border, Font, PatternFill, Side
from openpyxl.utils import get_column_letter


def export_to_excel(rooms: list[dict], output_path: str) -> None:
    wb = Workbook()
    ws = wb.active
    ws.title = "Místnosti"

    hdr_fill = PatternFill("solid", fgColor="155AEF")
    hdr_font = Font(bold=True, color="FFFFFF", size=10)
    hdr_align = Alignment(horizontal="center", vertical="center")
    row_border = Border(
        bottom=Side(style="thin", color="D0D5DC"),
        right=Side(style="thin", color="D0D5DC"),
    )
    alt_fill = PatternFill("solid", fgColor="F9FAFB")

    headers = ["Č. místnosti", "Popis / účel", "Zdroj", "Spolehlivost"]
    widths = [16, 52, 14, 16]

    for col, (hdr, w) in enumerate(zip(headers, widths), 1):
        c = ws.cell(row=1, column=col, value=hdr)
        c.font = hdr_font
        c.fill = hdr_fill
        c.alignment = hdr_align
        ws.column_dimensions[get_column_letter(col)].width = w

    ws.row_dimensions[1].height = 22
    ws.freeze_panes = "A2"

    for row_num, room in enumerate(sorted(rooms, key=lambda r: str(r.get("room", ""))), 2):
        values = [
            room.get("room", ""),
            room.get("description", ""),
            "Pravidla" if room.get("source") == "rule" else "AI",
            f"{room.get('confidence', 1.0) * 100:.0f} %",
        ]
        for col, val in enumerate(values, 1):
            c = ws.cell(row=row_num, column=col, value=val)
            c.alignment = Alignment(vertical="center")
            c.border = row_border
            if row_num % 2 == 0:
                c.fill = alt_fill

    wb.save(output_path)
