from typing import List

from fastapi import APIRouter, HTTPException

from src.db.models.admin import MailAccount
from src.db.session import SessionDep
from src.schema.admin.mailaccount import MailAccountResponse, to_response

router = APIRouter()

@router.get("/mailaccounts", response_model=List[MailAccountResponse])
def get_all_mailaccounts(db: SessionDep) -> List[MailAccountResponse]:
    """
    return all MailAccounts as JSON.
    """
    results: List[MailAccountResponse] = []
    mailaccounts = db.query(MailAccount).all()
    for mailaccount in mailaccounts:
        response = to_response(mailaccount)
        results.append(response)
    return results

@router.get("/mailaccounts/{mailaccount_id}", response_model=MailAccountResponse)
def get_mailaccount(mailaccount_id: str, db: SessionDep) -> MailAccountResponse:
    """
    return MailAccounts by id.
    """
    mailaccount = db.get(MailAccount, mailaccount_id)
    if mailaccount is None:
        raise HTTPException(status_code=409, detail="Mailaccount could not be found")
    response = to_response(mailaccount)
    return response
