from sqlalchemy.orm import Session
from fastapi import HTTPException
import app.models as models
import app.schemas as schemas
import app.services as services
import os
from pathlib import Path

def update_session(db: Session, session_id: int, session_update: schemas.SessionUpdate, user_id: int):
    db_session = db.query(models.Session).filter(models.Session.id == session_id).first()
    if not db_session:
        raise HTTPException(status_code=404, detail="Sesión no encontrada")
    # Only therapist or patient may update the session
    if user_id not in (db_session.patient_id, db_session.therapist_id):
        raise HTTPException(status_code=403, detail="No tienes permiso para actualizar esta sesión")
    # Update fields if provided
    if session_update.start_date is not None:
        db_session.start_date = session_update.start_date
    if session_update.end_date is not None:
        db_session.end_date = session_update.end_date
    # Validate dates after update
    if db_session.start_date >= db_session.end_date:
        raise HTTPException(status_code=400, detail="La fecha de inicio debe ser anterior a la fecha de fin")
    db.commit()
    db.refresh(db_session)
    return db_session

def delete_session(db: Session, session_id: int, user_id: int, user_type: str):
    db_session = db.query(models.Session).filter(models.Session.id == session_id).first()
    if not db_session:
        raise HTTPException(status_code=404, detail="Sesión no encontrada")
    if user_type != 'therapist' or user_id != db_session.therapist_id:
        raise HTTPException(status_code=403, detail="Solo el terapeuta de esta sesión puede eliminarla")
    db.delete(db_session)
    db.commit()
    return {"detail": "Session deleted successfully"}
from sqlalchemy.orm import Session
from fastapi import HTTPException
import app.models as models
import app.schemas as schemas
import app.services as services
from datetime import datetime


def finalize_expired_sessions(db: Session, user_id: int | None = None):
    """Auto-finalize sessions that passed their scheduled end_date without being ended.

    If user_id is provided, scope to sessions where the user is patient or therapist
    to minimize the update set for per-request calls.
    """
    query = db.query(models.Session).filter(
            models.Session.ended_at == None,
            models.Session.end_date != None,
            models.Session.end_date <= datetime.utcnow()
    )

    if user_id is not None:
        query = query.filter(
            (models.Session.patient_id == user_id) | (models.Session.therapist_id == user_id)
        )

    to_close = query.all()
    if not to_close:
        return 0

    for session in to_close:
        session.ended_at = session.end_date

    db.commit()
    return len(to_close)


def create_session_for_users(db: Session, patient_id: int, therapist_id: int, session: schemas.SessionCreate):
    patient = db.query(models.Patient).filter(models.Patient.id == patient_id).first()
    therapist = db.query(models.Therapist).filter(models.Therapist.id == therapist_id).first()

    if not patient or not therapist:
        raise HTTPException(status_code=404, detail="Paciente o Terapeuta no encontrado")  
    
    # assign start/end directly from Pydantic model (they are Optional[datetime])
    db_session = models.Session(
        patient_id=patient.id,
        therapist_id=therapist.id,
        start_date=session.start_date,
        end_date=session.end_date,
    )
    db.add(db_session)
    db.commit()
    db.refresh(db_session)
    return db_session


def end_session(db: Session, session_id: int):
    """
    Mark a session as ended by setting `ended_at` to now.
    """
    db_session = db.query(models.Session).filter(models.Session.id == session_id).first()
    if not db_session:
        raise HTTPException(status_code=404, detail="Sesión no encontrada")
    if db_session.ended_at is not None:
        raise HTTPException(status_code=400, detail="La sesión ya ha finalizado")
    db_session.ended_at = datetime.utcnow()
    db.commit()
    db.refresh(db_session)
    return db_session

def get_images_for_session(db: Session, session_id: int):
    """Retrieve all images for a given session."""
    db_session = db.query(models.Session).filter(models.Session.id == session_id).first()
    if not db_session:
        raise HTTPException(status_code=404, detail="Sesión no encontrada")
    images = db.query(models.Image).filter(models.Image.session_id == session_id).all()
    return {
        "data": images,
        "count": len(images)
    }
