from typing import Optional

from sqlalchemy import select

from backend.app.db.connection import db_read_session
from backend.app.db.models.uploaded_file import (
    FileSourceEnum,
    FileTypeEnum,
    UploadedFile,
)


def get_uploaded_file_by_listing_type_and_source(
    hubspot_listing_id: int,
    file_type: FileTypeEnum,
    file_source: FileSourceEnum,
) -> Optional[UploadedFile]:
    with db_read_session() as session:
        statement = select(UploadedFile).where(
            UploadedFile.hubspot_listing_id == hubspot_listing_id,
            UploadedFile.file_type == file_type,
            UploadedFile.file_source == file_source,
        )

        return session.exec(statement).one_or_none()
