# global imports
from db.entities.layer_adapter import LayerAdapter
from db.use_case_repository import UseCaseRepository
from db.table_repository import TableRepository
from db.repository import Repository
from services.layer_adapter_service import LayerAdapterService

import json
from flask import Response, request

use_case_repository = UseCaseRepository()
table_repository = TableRepository()
repository = Repository()

def all():
    return [use_case.to_serializable_dict() for use_case in use_case_repository.all()]

def delete_all():
    use_case_repository.delete_all()
    repository.delete_all()
    table_repository.delete_all()
    return Response(status=200)

def add():
    body = request.json

    if "name" not in body.keys():
        return Response(status=400, response="Field missing! Fields required: (name)")
    
    name = body["name"]

    # check if use-case exists
    reference = use_case_repository.get_by_name(name)
    if not reference == None:
        return Response(status=400, response="Use-Case already exists!")
    
    use_case_repository.put(name)
    return Response(status=200)
