
from typing import Union, Optional, List
from pydantic import BaseModel
from fastapi import FastAPI, Request, Depends
from CommonCode.settings import get_logger
import logging
from fastapi.responses import StreamingResponse
import os
import sys
import json
import time
from util import embed_scores as ES
from fastapi_server_session import SessionManager, RedisSessionInterface, Session
import redis
from datetime import timedelta

app = FastAPI()

session_manager = SessionManager(
    interface=RedisSessionInterface(redis.from_url("redis://localhost"))
)

logger = get_logger(__name__,'/var/log/vector_search_logs/main_embed_scores', stdout=True, systemd=False, level = logging.INFO)
r = redis.Redis(host='localhost', port=6379, db=15)


class VideosPostRequest(BaseModel):
    query: str = "A cat and a human"
    threshold: float = 0.10
    c_dirs: Optional[List[str]] = None
    task_id: str = 'compute_log'

@app.post("/videos.json")
async def videos_json(
    vpr: VideosPostRequest,
    session: Session = Depends(session_manager.use_session),
):

    query = vpr.query
    threshold = vpr.threshold
    c_dirs = vpr.c_dirs
    task_id = vpr.task_id
    if c_dirs is None:
        c_dirs = [
            # "/mnt/hdd_24tb_1/videos/ftp/leopards2/2025/08/26",
            # "/srv/ftp_tcc/leopards1/2025/08/27",
            # "/srv/ftp_tcc/leopards1/2025/08/28",
            # "/srv/ftp_tcc/leopards1/2025/08/29",
            # "/srv/ftp_tcc/leopards1/2025/08/30",
            # "/srv/ftp_tcc/leopards1/2025/08/31",
            # "/srv/ftp_tcc/leopards1/2025/09/01",
            # "/srv/ftp_tcc/leopards1/2025/09/02",
            # "/srv/ftp_tcc/leopards1/2025/09/03",
            # "/srv/ftp_tcc/leopards1/2025/09/04",
            # "/srv/ftp_tcc/leopards1/2025/09/05",
            # "/srv/ftp_tcc/leopards1/2025/09/06",
            # "/srv/ftp_tcc/leopards1/2025/09/07",
            "/srv/ftp_tcc/leopards1/2025/09/08",
            "/srv/ftp_tcc/leopards1/2025/09/09",
            "/srv/ftp_tcc/leopards1/2025/09/10",
            "/srv/ftp_tcc/leopards1/2025/09/11",            
        ]
    

    # print(','.join([str(x) for x in c_dirs]))
    # message = {'task':'SCHEDULED','when':[str(x) for x in c_dirs], 'time':time.time()}
    # r.rpush(task_id, json.dumps(message))?


    for x in c_dirs:
        message = {'task':'QUEUEING', 'when': str(x), 'time': time.time()}
        r.rpush(task_id, json.dumps(message))

    folder_scores = ES.calculate_embedding_score_in_folders(
        tuple(c_dirs), threshold=threshold, query=query, redis_key = task_id)
    # if p_hits != ES.calculate_embedding_score_in_folders.cache_info().hits:
    #     logger.info("FROM CACHE")
    # else:pp
    #     logger.info("COMPUTED FROM SCRATCH")



    folder_scores["breaks"] = ES.add_breaks_between_videos(folder_scores)
    folder_scores['videos'] = ES.collapse_scores_to_maxmin_avg(folder_scores)
    return folder_scores


class ClickEvent(BaseModel):
    timestamp: float


class ClickResponse(BaseModel):
    path: str
    timeoffset: float
