# Path: src/schemas/messages.py
# class SendMethod(str, Enum):
#     """
#     Should match SEND_METHODS sql enum
#     """
# 
#     email_mandrill = 'email-mandrill'
#     email_ses = 'email-ses'
#     email_test = 'email-test'
#     sms_messagebird = 'sms-messagebird'
#     sms_test = 'sms-test'
# 
# class SubaccountModel(BaseModel):
#     company_code: str = ...
#     company_name: str = None
# 
# Path: src/utils.py
# class AdminAuth:
#     def __init__(self, request: Request):
#         if request.headers.get('Authorization', '') != glove.settings.auth_key:
#             raise HttpForbidden('Invalid token')


import json
import logging
from buildpg.asyncpg import BuildPgConnection
from fastapi import APIRouter, Depends
from foxglove import glove
from foxglove.db.middleware import get_db
from foxglove.exceptions import HttpBadRequest, HttpConflict, HttpNotFound
from foxglove.route_class import KeepBodyAPIRoute
from httpx import Response
from starlette.responses import JSONResponse
from typing import Optional
from src.schemas.messages import SendMethod, SubaccountModel
from src.utils import AdminAuth


logger = logging.getLogger('views.subaccounts')
app = APIRouter(route_class=KeepBodyAPIRoute, dependencies=[Depends(AdminAuth)])


@app.post('/create-subaccount/{method}/')
async def create_subaccount(method: SendMethod, m: Optional[SubaccountModel] = None):
    if method != SendMethod.email_mandrill:
        return JSONResponse({'message': f'no subaccount creation required for "{method}"'})
    assert m

    r: Response = await glove.mandrill.post(

# Path: src/schemas/messages.py
# class SendMethod(str, Enum):
#     """
#     Should match SEND_METHODS sql enum
#     """
# 
#     email_mandrill = 'email-mandrill'
#     email_ses = 'email-ses'
#     email_test = 'email-test'
#     sms_messagebird = 'sms-messagebird'
#     sms_test = 'sms-test'
# 
# class SubaccountModel(BaseModel):
#     company_code: str = ...
#     company_name: str = None
# 
# Path: src/utils.py
# class AdminAuth:
#     def __init__(self, request: Request):
#         if request.headers.get('Authorization', '') != glove.settings.auth_key:
#             raise HttpForbidden('Invalid token')


# Path: src/views/subaccounts.py
import json
import logging
from buildpg.asyncpg import BuildPgConnection
from fastapi import APIRouter, Depends
from foxglove import glove
from foxglove.db.middleware import get_db
from foxglove.exceptions import HttpBadRequest, HttpConflict, HttpNotFound
from foxglove.route_class import KeepBodyAPIRoute
from httpx import Response
from starlette.responses import JSONResponse
from typing import Optional
from src.schemas.messages import SendMethod, SubaccountModel
from src.utils import AdminAuth



logger = logging.getLogger('views.subaccounts')
app = APIRouter(route_class=KeepBodyAPIRoute, dependencies=[Depends(AdminAuth)])


@app.post('/create-subaccount/{method}/')
async def create_subaccount(method: SendMethod, m: Optional[SubaccountModel] = None):
    if method != SendMethod.email_mandrill:
        return JSONResponse({'message': f'no subaccount creation required for "{method}"'})
    assert m

    r: Response = await glove.mandrill.post(

        'subaccounts/add.json', id=m.company_code, name=m.company_name, allowed_statuses=(200, 500), timeout_=12