import httpx
from urllib.parse import urlsplit
from fastapi import APIRouter, HTTPException
from fastapi.responses import Response, StreamingResponse
from starlette.concurrency import run_in_threadpool

from hermes_hls_registry_v2 import HLSRegistryV2
from hermes_hls_manifest import transformar_manifest
from hermes_hls_manifest_v4_lab import transformar_manifest_v4
from hermes_hls_fetch import descargar_hls
from hermes_hls_stream_v3 import preparar_segmento, transmitir_preparado
from hermes_hls_security import validar_destino

router = APIRouter(prefix="/api/player")
registro = HLSRegistryV2(ttl=120)
CANAL = "ppv/lagranja1"


def registrar_maestro(url):
    url = validar_destino(url)
    identidad = ("master", CANAL, url)
    token = registro.registrar(url, identidad)
    return "/api/player/resource/" + token


def registrar_segmento(url, identidad):
    url = validar_destino(url)
    identidad_segura = ("segmento", CANAL, identidad)
    token = registro.registrar(url, identidad_segura)
    return "/api/player/resource/" + token


async def responder(url):
    url = validar_destino(url)

    if not urlsplit(url).path.endswith(".m3u8"):
        cliente, respuesta = await preparar_segmento(url)
        return StreamingResponse(
            transmitir_preparado(cliente, respuesta),
            media_type="video/mp2t",
            headers={"Cache-Control": "no-store"}
        )

    resultado = await run_in_threadpool(
        descargar_hls, url, "manifest"
    )

    contenido = resultado["contenido"]
    origen = resultado["url_final"]

    if not contenido.startswith(b"#EXTM3U"):
        raise ValueError("Manifiesto HLS invalido")

    texto = contenido.decode("utf-8-sig")

    if "#EXT-X-MEDIA-SEQUENCE:" in texto:
        transformado = transformar_manifest_v4(
            texto, origen, registrar_segmento, CANAL
        )
    else:
        transformado = transformar_manifest(
            texto, origen, registrar_maestro
        )

    return Response(
        content=transformado,
        media_type="application/vnd.apple.mpegurl",
        headers={"Cache-Control": "no-store"}
    )


@router.get("/play/ppv/lagranja1.m3u8")
async def reproducir():
    from hermes_player_gateway import resolver_canal

    try:
        url = await run_in_threadpool(
            resolver_canal, "ppv", "lagranja1"
        )
        return await responder(url)
    except Exception:
        raise HTTPException(
            status_code=502,
            detail="No se pudo iniciar reproduccion"
        )


@router.get("/resource/{identificador}")
async def recurso(identificador: str):
    try:
        url = registro.resolver(identificador)
    except ValueError:
        raise HTTPException(
            status_code=404,
            detail="Recurso no disponible"
        )

    try:
        return await responder(url)
    except httpx.HTTPStatusError as e:
        raise HTTPException(
            status_code=502,
            detail="Origen HLS no disponible"
        )
    except Exception:
        raise HTTPException(
            status_code=502,
            detail="Error de recurso HLS"
        )
