from __future__ import annotations

from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session

from app.core.database import get_db
from app.core.dependencies import get_current_admin
from app.core.responses import api_success
from app.models.admin import Admin
from app.schemas.admin import AdminLoginRequest, AdminLoginResponse, AdminRead
from app.services.admin_service import AdminAuthService

router = APIRouter()


@router.post("/login")
def login(payload: AdminLoginRequest, db: Session = Depends(get_db)) -> dict:
    result = AdminAuthService.login(db, payload.username, payload.password)
    return api_success(AdminLoginResponse.model_validate(result).model_dump(mode="json"))


@router.get("/profile")
def profile(current_admin: Admin = Depends(get_current_admin)) -> dict:
    return api_success(AdminRead.model_validate(current_admin).model_dump())
