import pymongo
import typer

from apps.users.config import users_settings
from fastapi_mongodb.db import db_handler
from fastapi_mongodb.helpers import MakeAsync

from typing import Optional
from pydantic import BaseModel, EmailStr

from apps.users.schemas import UserCreateSchema

import requests
import json

BASE_URL = 'http://localhost:9000/users/'


__all__ = ["users_commands"]

users_commands = typer.Typer(name="users")


async def setup_app():
    await db_handler.create_collection(name=users_settings.USERS_COL)

    indexes = [
        pymongo.IndexModel(
            keys=[("email", pymongo.ASCENDING)],
            name="email",
            unique=True,
            background=True,
        )
    ]
    result = await db_handler.create_indexes(col_name=users_settings.USERS_COL, indexes=indexes)
    for index_name in result:
        typer.secho(
            message=f"Index: '{index_name}' for collection '{users_settings.USERS_COL}' created successfully.",
            fg=typer.colors.GREEN,
        )

async def add_user(email: str,  password: str, name: Optional[str] , lastname: Optional[str]):
    
    url = BASE_URL
    
    payload = {
        "email": email,
        "password": password,
        "first_name": name,
        "last_name": lastname
    }
    
    headers = {
        'content-type': 'application/json',
        'accept': 'application/json',
    }
    
    response_decoded_json = requests.post(url, json=payload, headers=headers)
    response_json = response_decoded_json.json()
    response_json = json.dumps(response_json, indent=4, sort_keys=True)

    if response_decoded_json.status_code  == requests.codes.created:
        typer.secho(
            message=f"user: 'user {email}' created successfully.",
            fg=typer.colors.GREEN,
        )
        typer.secho(
            message=str(response_json),
            fg=typer.colors.GREEN,
        )
    else:
        typer.secho(
            message=f"user: 'user {email}' not created.",
            fg=typer.colors.RED,
        )

        typer.secho(
            message=str(response_json),
            fg=typer.colors.RED,
        )



@users_commands.command(name="setup")
@MakeAsync()
async def setup():
    await setup_app()


@users_commands.command(name="create")
@MakeAsync()
async def create_user(email: str, password: str, name: Optional[str] = typer.Argument(None) , lastname: Optional[str] = typer.Argument(None)):
    await add_user(email, password, name, lastname)


import pymongo
import typer

from apps.users.config import users_settings
from fastapi_mongodb.db import db_handler
from fastapi_mongodb.helpers import MakeAsync

from typing import Optional
from pydantic import BaseModel, EmailStr

from apps.users.schemas import UserCreateSchema

import requests
import json

BASE_URL = 'http://localhost:9000/users/'


__all__ = ["users_commands"]

users_commands = typer.Typer(name="users")


async def setup_app():
    await db_handler.create_collection(name=users_settings.USERS_COL)

    indexes = [
        pymongo.IndexModel(
            keys=[("email", pymongo.ASCENDING)],
            name="email",
            unique=True,
            background=True,
        )
    ]
    result = await db_handler.create_indexes(col_name=users_settings.USERS_COL, indexes=indexes)
    for index_name in result:
        typer.secho(
            message=f"Index: '{index_name}' for collection '{users_settings.USERS_COL}' created successfully.",
            fg=typer.colors.GREEN,
        )

async def add_user(email: str,  password: str, name: Optional[str] , lastname: Optional[str]):
    
    url = BASE_URL
    
    payload = {
        "email": email,
        "password": password,
        "first_name": name,
        "last_name": lastname
    }
    
    headers = {
        'content-type': 'application/json',
        'accept': 'application/json',
    }
    
    response_decoded_json = requests.post(url, json=payload, headers=headers)
    response_json = response_decoded_json.json()
    response_json = json.dumps(response_json, indent=4, sort_keys=True)

    if response_decoded_json.status_code  == requests.codes.created:
        typer.secho(
            message=f"user: 'user {email}' created successfully.",
            fg=typer.colors.GREEN,
        )
        typer.secho(
            message=str(response_json),
            fg=typer.colors.GREEN,
        )
    else:
        typer.secho(
            message=f"user: 'user {email}' not created.",
            fg=typer.colors.RED,
        )

        typer.secho(
            message=str(response_json),
            fg=typer.colors.RED,
        )



@users_commands.command(name="setup")
@MakeAsync()
async def setup():
    await setup_app()


@users_commands.command(name="create")
@MakeAsync()
async def create_user(email: str, password: str, name: Optional[str] = typer.Argument(None) , lastname: Optional[str] = typer.Argument(None)):
    await add_user(email, password, name, lastname)

