from datetime import datetime

from panther import version
from panther.utils import generate_secret_key

apis_py = """from datetime import timedelta

from panther import status
from panther.app import API
from panther.response import Response
from panther.utils import timezone_now


# More Info: https://PantherPy.GitHub.io/api/
@API(cache=timedelta(minutes=1))
async def hello_world_api():
    current_time = timezone_now().isoformat()
    return Response(
        data={'message': f'Hello from Panther! 🐾 | {current_time}'},
        status_code=status.HTTP_200_OK
    )
"""

models_py = """\"""
More Info: https://PantherPy.GitHub.io/model/
\"""
from panther.db import Model
"""

serializers_py = """\"""
More Info: https://PantherPy.GitHub.io/serializer/
\"""
from panther.serializer import ModelSerializer
"""

middlewares_py = """\"""
More Info: https://PantherPy.GitHub.io/middlewares/
\"""
"""

app_urls_py = """from app.apis import hello_world_api

url_routing = {
    '/': hello_world_api,
}
"""

configs_py = """\"""
{PROJECT_NAME} Project (Generated by Panther on %s)
\"""{IMPORT_PATH}{IMPORT_COMPLETE_LOAD_ENV}
{BASE_DIR}{SECRET_KEY}{DATABASE}{REDIS}{USER_MODEL}{AUTHENTICATION}

# More Info: https://PantherPy.GitHub.io/timezone/
TIMEZONE = 'UTC'

# More Info: https://PantherPy.GitHub.io/urls/
URLs = 'core.urls.url_routing'
""" % datetime.now().date().isoformat()

env = """SECRET_KEY='%s'
""" % generate_secret_key()

main_py = """from panther import Panther

app = Panther(__name__)
"""

urls_py = """from app.urls import url_routing as app_url_routing

url_routing = {
    '/': app_url_routing,
}
"""

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

.env
*.pdb
"""

requirements = """panther==%s
""" % version()

TEMPLATE = {
    'app': {
        '__init__.py': '',
        'apis.py': apis_py,
        'models.py': models_py,
        'serializers.py': serializers_py,
        'urls.py': app_urls_py,
    },
    'core': {
        '__init__.py': '',
        'configs.py': configs_py,
        'middlewares.py': middlewares_py,
        'urls.py': urls_py,
    },
    'main.py': main_py,
    '.env': env,
    '.gitignore': git_ignore,
    'requirements.txt': requirements,
}

single_main_py = """\"""
{PROJECT_NAME} Project (Generated by Panther on %s)
\"""
from datetime import timedelta{IMPORT_PATH}

from panther import Panther, status
from panther.app import API
from panther.response import Response
from panther.utils import {IMPORT_LOAD_ENV}timezone_now
{BASE_DIR}{SECRET_KEY}{DATABASE}{REDIS}{USER_MODEL}{AUTHENTICATION}

# More Info: https://PantherPy.GitHub.io/timezone/
TIMEZONE = 'UTC'


# More Info: https://PantherPy.GitHub.io/api/
@API(cache=timedelta(minutes=1))
async def hello_world_api():
    current_time = timezone_now().isoformat()
    return Response(
        data={'message': f'Hello from Panther! 🐾 | {current_time}'},
        status_code=status.HTTP_200_OK
    )


url_routing = {
    '/': hello_world_api,
}

app = Panther(__name__, configs=__name__, urls=url_routing)
""" % datetime.now().date().isoformat()

SINGLE_FILE_TEMPLATE = {
    'main.py': single_main_py,
    '.env': env,
    '.gitignore': git_ignore,
    'requirements.txt': requirements,
}

DATABASE_PANTHERDB_PART = """

# More Info: https://PantherPy.GitHub.io/database/#pantherdb
DATABASE = {
    'engine': {
        'class': 'panther.db.connections.PantherDBConnection',
        'path': BASE_DIR / 'database.pdb',
    }
}"""

DATABASE_MONGODB_PART = """

# More Info: https://PantherPy.GitHub.io/database/#mongodb
DATABASE = {
    'engine': {
        'class': 'panther.db.connections.MongoDBConnection',
        'host': '127.0.0.1',
        'port': 27017,
        'database': '{PROJECT_NAME}'
    }
}"""

REDIS_PART = """

# More Info: https://PantherPy.GitHub.io/redis/
REDIS = {
    'class': 'panther.db.connections.RedisConnection',
    'host': '127.0.0.1',
    'port': 6379,
}"""

USER_MODEL_PART = """

# More Info: https://PantherPy.GitHub.io/user_model/
USER_MODEL = 'panther.db.models.BaseUser'"""

AUTHENTICATION_PART = """

# More Info: https://PantherPy.GitHub.io/authentications/
AUTHENTICATION = 'panther.authentications.JWTAuthentication'"""

SECRET_KEY_PART = """

env = load_env(BASE_DIR / '.env')

SECRET_KEY = env['SECRET_KEY']"""

BASE_DIR_PART = """
BASE_DIR = Path(__name__).resolve().parent"""

IMPORT_PATH_PART = """
from pathlib import Path"""

IMPORT_LOAD_ENV_PART = """load_env, """

IMPORT_COMPLETE_LOAD_ENV_PART = """
from panther.utils import load_env"""
