from database.connection import get_db


async def add_attachment(
    ticket_id: int,
    attachment_type: str,
    attachment_url: str | None,
    filename: str | None = None,
) -> None:
    db = get_db()
    await db.execute(
        """
        INSERT INTO ticket_attachments (ticket_id, attachment_type, attachment_url, filename)
        VALUES (?, ?, ?, ?)
        """,
        (ticket_id, attachment_type, attachment_url, filename),
    )
    await db.commit()


async def get_attachments(ticket_id: int) -> list[dict]:
    db = get_db()
    async with db.execute(
        "SELECT * FROM ticket_attachments WHERE ticket_id = ? ORDER BY created_at",
        (ticket_id,),
    ) as cur:
        rows = await cur.fetchall()
    return [dict(r) for r in rows]
