from datetime import datetime


apis_py = """from panther.app import API
from panther.request import Request
from panther.response import Response

from app.models import User
from app.serializers import UserInputSerializer, UserOutputSerializer


@API.post(input_model=UserInputSerializer)
async def create_user(request: Request):
    obj: UserInputSerializer = request.data
    User.create(username=obj.username, password=obj.password)
    return Response(status_code=201)


@API.get(output_model=UserOutputSerializer)
async def get_users():
    users = User.list()
    return Response(data=users)

"""

models_py = """from panther.db import BaseModel

class User(BaseModel):
    username: str
    password: str

"""

serializers_py = """from pydantic import BaseModel, constr


class UserInputSerializer(BaseModel):
    username: str
    password: constr(min_length=8)


class UserOutputSerializer(BaseModel):
    id: str
    username: str

"""

app_urls_py = """from app.apis import create_user, get_users

urls = {
    'create/': create_user,
    'list/': get_users,
}

"""

configs_py = """\"""
Generated by Panther on %s
\"""

from pathlib import Path
from dotenv import dotenv_values


DEBUG = True 
BASE_DIR = Path(__name__).resolve().parent
env = dotenv_values(BASE_DIR / '.env')

DB_NAME = env['DB_NAME']
SECRET_KEY = env['SECRET_KEY']

Middlewares = [
    ('panther/middlewares/db.py', {'url': f'tinydb://{BASE_DIR}/{DB_NAME}.json'}),
]

URLs = 'core/urls.py'
""" % datetime.now().date().isoformat()

env = """
SECRET_KEY = 'THIS_IS_THE_SECRET_SECRET_KEY'

DB_NAME = '{DATABASE_NAME}'

"""

main_py = """from panther import Panther

app = Panther(__name__)

"""

urls_py = """from app.urls import urls as app_urls

urls = {
    '': app_urls, 
}

"""

git_ignore = """__pycache__/
.idea/
.venv/
__pycache__/
.env

"""

Template = {
    'app': {
        'apis.py': apis_py,
        'models.py': models_py,
        'serializers.py': serializers_py,
        'urls.py': app_urls_py,
    },
    'core': {
        'configs.py': configs_py,
        'urls.py': urls_py,
    },
    'main.py': main_py,
    '.env': env,
    '.gitignore': git_ignore,
}

# TODO: Add .gitignore

# TODO: Add core/middlewares.py to Template
