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, UserIDSerializer


@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)


@API.put(input_model=UserIDSerializer, output_model=UserOutputSerializer)
async def update_user(request: Request):
    user = User.update_one(id=request.data.id)
    user.update(password='Another-Secure-Password')
    return Response(data={'detail': 'Updated Successfully.'}, status_code=202)


@API.delete()
async def delete_user(request: Request):
    user = User.get_one(id=request.data.id)
    if not user:
        return Response(data={'detail': 'User Not Found.'}, status_code=404)

    if user.delete():
        return Response(status_code=204)
    else:
        return Response(data={'detail': "couldn't delete the user"}, status_code=409)

"""

models_py = """from panther.db import MongoBaseModel

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

"""

serializers_py = """from pydantic import BaseModel, constr


class UserIDSerializer(BaseModel):
    id: str


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, update_user, delete_user

urls = {
    'create/': create_user,
    'list/': get_users,
    'update/': update_user,
    'delete/': delete_user,
}

"""

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, 
}

"""

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,
    },
    '.env': env,
    'main.py': main_py,
}

# TODO: Add .gitignore

# TODO: Add core/middlewares.py to Template
