from typing import Any, List
from fastapi.responses import RedirectResponse
from fastapi import APIRouter, Body, Depends, HTTPException, Request
from fastapi.encoders import jsonable_encoder
from pydantic.networks import EmailStr
from sqlalchemy.orm import Session

from app import crud, models, schemas
from app.api import deps
from app.core.config import settings

from datetime import datetime

router = APIRouter()

@router.get("/add-time")
def add_time(
    *,
    db: Session = Depends(deps.get_db),
    current_user: models.User = Depends(deps.get_current_active_user),
    ser_no: models.SerialNumber = Depends(deps.get_avairable_serial_number)
)-> Any:

  
    user_in = schemas.UserUpdate(available_time=current_user.available_time+ser_no.time)
    crud.user.update(db, db_obj=current_user, obj_in=user_in)
    
    ser_no_in = schemas.SerialNumberUpdate(code=ser_no.code, is_used=True, used_datetime=str(datetime.now()), owner_id=current_user.id)
    crud.serial_number.update(db, db_obj=ser_no, obj_in=ser_no_in)

    return {"time_added": ser_no.time}

@router.get("/add-time-no-token")
def add_time_no_token(
    request: Request,
    ser_no: models.SerialNumber = Depends(deps.get_avairable_serial_number)
)-> Any:
    print(f"{request.base_url}login?ser_no={ser_no.code}")
    #return RedirectResponse(f"{request.base_url}login?ser_no={ser_no.code}")
    return RedirectResponse(f"http://localhost:5173/login?add-time-code={ser_no.code}")

