from datetime import datetime
from fastapi import APIRouter, Request
from ..classes.jwt_authenticator import JWTAuthenticator
from ..repositories import reply_like_repository
from ..schemas.reply_like_schema import RequestSchema, ResponseSchema

router = APIRouter(
    prefix='/reply-likes',
    tags=['reply-likes'],
)

# Index
@router.get('/')
async def index():
    pass

# Store a new reply like, or update if exists
@router.post('/', response_model=ResponseSchema, status_code=200)
async def store(request: Request, req: RequestSchema):
    authenticator = JWTAuthenticator()

    # get a user from http headers
    auth = await authenticator.get_current_user(request)

    # check the record
    reply_like = await reply_like_repository.select_one(user_id=auth['id'], reply_id=req.reply_id)

    if reply_like is None:
        # insert a new record
        await reply_like_repository.insert(user_id=auth['id'], reply_id=req.reply_id)
        is_reply_liked = True
        
    else:
        if reply_like.deleted_at is not None:
            # like the reply
            await reply_like_repository.update(user_id=auth['id'], reply_id=req.reply_id, deleted_at=None)
            is_reply_liked = True
        else:
            # unlike the reply
            await reply_like_repository.update(user_id=auth['id'], reply_id=req.reply_id, deleted_at=datetime.now())
            is_reply_liked = False

    response = ResponseSchema(is_liked=is_reply_liked)
    return response

from datetime import datetime
from fastapi import APIRouter, Request
from ..classes.jwt_authenticator import JWTAuthenticator
from ..repositories import reply_like_repository
from ..schemas.reply_like_schema import RequestSchema, ResponseSchema

router = APIRouter(
    prefix='/reply-likes',
    tags=['reply-likes'],
)

# Index
@router.get('/')
async def index():
    pass

# Store a new reply like, or update if exists
@router.post('/', response_model=ResponseSchema, status_code=200)
async def store(request: Request, req: RequestSchema):
    authenticator = JWTAuthenticator()

    # get a user from http headers
    auth = await authenticator.get_current_user(request)

    # check the record
    reply_like = await reply_like_repository.select_one(user_id=auth['id'], reply_id=req.reply_id)

    if reply_like is None:
        # insert a new record
        await reply_like_repository.insert(user_id=auth['id'], reply_id=req.reply_id)
        is_reply_liked = True
        
    else:
        if reply_like.deleted_at is not None:
            # like the reply
            await reply_like_repository.update(user_id=auth['id'], reply_id=req.reply_id, deleted_at=None)
            is_reply_liked = True
        else:
            # unlike the reply
            await reply_like_repository.update(user_id=auth['id'], reply_id=req.reply_id, deleted_at=datetime.now())
            is_reply_liked = False

    response = ResponseSchema(is_liked=is_reply_liked)
    return response

# Index # Store a new reply like, or update if exists # get a user from http headers # check the record # insert a new record # like the reply # unlike the reply