from fastapi import APIRouter, Depends, HTTPException, Path
from sqlmodel import Session, select
from sqlalchemy.exc import IntegrityError
from typing import List
import datetime as dt

from app.src.common.security import get_current_user
from app.src.common.utils import profiling_api
from app.src.models.app_user import AppUser
from app.src.models.tag import Tag, TagRead, TagCreate, TagUpdate
from app.src.db.engine import get_session


router = APIRouter()


async def get_tag_or_404(
    *,
    session: Session = Depends(get_session),
    tag_id: int = Path(..., ge=1),
    current_user: AppUser = Depends(get_current_user),
):
    start_time = dt.datetime.now()
    try:
        db_tag = session.get(Tag, tag_id)
        if db_tag:
            return {
                "db_tag": db_tag,
                "username": current_user.username,
                "start_time": start_time,
            }
        else:
            raise HTTPException(status_code=404, detail="Tag not found")
    except KeyError:
        raise HTTPException(status_code=400, detail="Tag not found")


async def get_tag_by_name_or_404(
    *,
    session: Session = Depends(get_session),
    tag_name: str,
    current_user: AppUser = Depends(get_current_user),
):
    start_time = dt.datetime.now()
    try:
        db_tag = session.exec(