from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession

from app.database import get_db
from app.schemas.common import ApiResponse
from app.schemas.token import RefreshRequest
from app.schemas.user import (
    LoginByCodeRequest,
    LoginBySmsRequest,
    SendCodeRequest,
    SendSmsRequest,
    UserLogin,
    UserRegister,
    UserRegisterBySms,
)
from app.services import auth_service

router = APIRouter()


@router.post("/send-code", response_model=ApiResponse)
async def send_code(data: SendCodeRequest, db: AsyncSession = Depends(get_db)):
    cooldown = await auth_service.send_register_code(db, data)
    return ApiResponse(data={"cooldown": cooldown})


@router.post("/send-login-code", response_model=ApiResponse)
async def send_login_code(data: SendCodeRequest, db: AsyncSession = Depends(get_db)):
    cooldown = await auth_service.send_login_code(db, data)
    return ApiResponse(data={"cooldown": cooldown})


@router.post("/send-sms-code", response_model=ApiResponse)
async def send_sms_code(data: SendSmsRequest, db: AsyncSession = Depends(get_db)):
    cooldown = await auth_service.send_register_sms_code(db, data)
    return ApiResponse(data={"cooldown": cooldown})


@router.post("/send-login-sms-code", response_model=ApiResponse)
async def send_login_sms_code(data: SendSmsRequest, db: AsyncSession = Depends(get_db)):
    cooldown = await auth_service.send_login_sms_code(db, data)
    return ApiResponse(data={"cooldown": cooldown})


@router.post("/register", response_model=ApiResponse)
async def register(data: UserRegister, db: AsyncSession = Depends(get_db)):
    user, tokens = await auth_service.register(db, data)
    return ApiResponse(data={"user": user, "tokens": tokens})


@router.post("/register-by-sms", response_model=ApiResponse)
async def register_by_sms(data: UserRegisterBySms, db: AsyncSession = Depends(get_db)):
    user, tokens = await auth_service.register_by_sms(db, data)
    return ApiResponse(data={"user": user, "tokens": tokens})


@router.post("/login", response_model=ApiResponse)
async def login(data: UserLogin, db: AsyncSession = Depends(get_db)):
    user, tokens = await auth_service.login(db, data)
    return ApiResponse(data={"user": user, "tokens": tokens})


@router.post("/login-by-code", response_model=ApiResponse)
async def login_by_code(data: LoginByCodeRequest, db: AsyncSession = Depends(get_db)):
    user, tokens = await auth_service.login_by_code(db, data)
    return ApiResponse(data={"user": user, "tokens": tokens})


@router.post("/login-by-sms", response_model=ApiResponse)
async def login_by_sms(data: LoginBySmsRequest, db: AsyncSession = Depends(get_db)):
    user, tokens = await auth_service.login_by_sms(db, data)
    return ApiResponse(data={"user": user, "tokens": tokens})


@router.post("/refresh", response_model=ApiResponse)
async def refresh(data: RefreshRequest, db: AsyncSession = Depends(get_db)):
    tokens = await auth_service.refresh(db, data.refresh_token)
    return ApiResponse(data=tokens)


@router.post("/logout", response_model=ApiResponse)
async def logout(data: RefreshRequest):
    await auth_service.logout(data.refresh_token)
    return ApiResponse(data=None)
