from layercake.dateutils import now
from layercake.dynamodb import DynamoDBPersistenceLayer, TransactItems

from models import Course, Org


def create_course(
    course: Course,
    org: Org,
    *,
    persistence_layer: DynamoDBPersistenceLayer,
):
    current_time = now()
    transact = TransactItems(persistence_layer.table_name)
    transact.put(
        item={
            'sk': '0',
            'tenant:org_id': {org.id},
            'create_date': current_time,
            **course.model_dump(),
        }
    )
    transact.put(
        item={
            'id': course.id,
            'sk': 'tenant',
            'org_id': org.id,
            'name': org.name,
            'create_date': current_time,
        }
    )
    return persistence_layer.transact_write_items(transact)
