"""
Service to write extracted data to Excel in triple-header format.
"""

import os
import openpyxl
from datetime import datetime

def write_excel_row(output_path: str, row_data: dict):
    """
    Append a new row to the Excel output file.
    If file doesn't exist, create with headers first.
    """
    file_exists = os.path.exists(output_path)

    if file_exists:
        wb = openpyxl.load_workbook(output_path)
        ws = wb.active
    else:
        wb = openpyxl.Workbook()
        ws = wb.active
        # TODO: Insert your triple-header rows here (3 rows of headers)
        ws.append(["Transaction", "Sales Tax", "DMV Fees", "Notes"])  

    # Write data row
    ws.append([
        row_data.get("Transaction ID", ""),
        row_data.get("Titling State", ""),
        row_data.get("Process Date", ""),
        row_data.get("Sales Tax", ""),
    ])

    wb.save(output_path)
    return output_path
