File size: 11,984 Bytes
fcd83bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
from fastapi import APIRouter, Depends, UploadFile, File, HTTPException
from sqlalchemy.orm import Session
from core.database import SessionLocal
from core.models.appointment import Appointment
from core.models.user import User
from utils.stt_processor import simulate_stt
from utils.specialist_predictor import predict_specialist
from datetime import datetime, timedelta
import shutil
import os

router = APIRouter()

def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()
from sqlalchemy import func
from datetime import datetime, timedelta
from sqlalchemy import func
from core.models.user import User
from core.models.appointment import Appointment

def round_up_to_next_slot(dt, slot_minutes=30):
    """Round datetime up to next 30-min aligned slot"""
    discard = timedelta(minutes=dt.minute % slot_minutes,
                        seconds=dt.second,
                        microseconds=dt.microsecond)
    dt += timedelta(minutes=slot_minutes) - discard if discard.total_seconds() > 0 else timedelta()
    return dt.replace(second=0, microsecond=0)

def find_doctor_and_slot_today(specialization, duration_minutes, db):
    today = datetime.today().date()
    now = datetime.now()
    aligned_now = round_up_to_next_slot(now)

    doctors = db.query(User).filter(
        func.lower(User.role) == 'specialist',
        func.lower(User.specialization) == specialization.lower()
    ).all()

    for doc in doctors:
        start = datetime.combine(today, doc.work_start)
        end = datetime.combine(today, doc.work_end)

        if aligned_now > end:
            continue

        current = max(start, aligned_now)

        # Get all today's appointments for this doctor
        appointments = db.query(Appointment).filter_by(
            specialist_id=doc.id,
            date=today
        ).order_by(Appointment.start_time).all()

        for appt in appointments:
            appt_start = datetime.combine(today, appt.start_time)
            appt_end = datetime.combine(today, appt.end_time)

            # Check for free gap before this appointment
            if (appt_start - current).total_seconds() >= duration_minutes * 60:
                return doc, current.time()

            current = max(current, appt_end)
            current = round_up_to_next_slot(current)

        # Check if there's free time after last appointment
        if (end - current).total_seconds() >= duration_minutes * 60:
            return doc, current.time()

    return None, None

@router.post("/appointments/voice")
async def create_appointment_from_voice(audio: UploadFile = File(...), db: Session = Depends(get_db)):
    temp_path = f"temp_{audio.filename}"
    with open(temp_path, "wb") as buffer:
        shutil.copyfileobj(audio.file, buffer)

    try:
        data = simulate_stt(temp_path)
        symptoms = data.get("symptoms", "")
        patient_name = data.get("patient_name", "Unknown")
        age = data.get("age", 0)

        specialist, score = predict_specialist(symptoms)
        print(specialist)
        doctor_exists = db.query(User).filter(
            func.lower(User.role) == 'specialist',
            func.lower(User.specialization) == specialist.lower()
        ).first()

        if not doctor_exists:
            return {
                "message": f"Doctor specialization '{specialist}' is not registered. Please try different symptoms or contact admin."
            }
        # Auto-book today only
        doc, slot = find_doctor_and_slot_today(specialist, 30, db)
        print(doc,slot)
        if not doc or not slot:
            return {
                "message": f"No available slots today for {specialist}. Please try later or choose another day."
            }

        # Create appointment with time and doctor
        today = datetime.today().date()
        start_time = slot
        end_time = (datetime.combine(today, slot) + timedelta(minutes=30)).time()

        new_appointment = Appointment(
            patient_name=patient_name,
            age=age,
            symptoms=symptoms,
            specialist=specialist,
            specialist_id=doc.id,
            date=today,
            start_time=start_time,
            end_time=end_time,
            status="confirmed"
        )

        db.add(new_appointment)
        db.commit()
        db.refresh(new_appointment)

        return {
            "message": "Appointment booked successfully from voice",
            "predicted_specialist": specialist,
            "similarity_score": round(score, 4),
            "appointment": {
                "id": new_appointment.id,
                "patient_name": patient_name,
                "age": age,
                "symptoms": symptoms,
                "specialist": specialist,
                "doctor": doc.full_name,
                "date": str(today),
                "time": start_time.strftime('%I:%M %p')
            }
        }

    finally:
        os.remove(temp_path)

from schemas.appointment import AppointmentManual

@router.post("/appointments/create")
async def create_appointment(data: AppointmentManual, db: Session = Depends(get_db)):
    # If specialist/doctor is NOT given, predict from symptoms
    if not data.specialist or not data.specialist_id:
        predicted_specialist, score = predict_specialist(data.symptoms)
        data.specialist = predicted_specialist

        doc, slot = find_doctor_and_slot_today(predicted_specialist, 30, db)
        if not doc or not slot:
            raise HTTPException(
                status_code=409,
                detail=f"No available doctors for {predicted_specialist} at this time."
            )

        specialist_id = doc.id
    else:
        # use provided doctor/specialist
        doc = db.query(User).filter_by(id=data.specialist_id, role="specialist").first()
        if not doc:
            raise HTTPException(status_code=404, detail="Specialist not found")
        
        # Check for available slot
        doc, slot = find_doctor_and_slot_today(doc.specialization, 30, db)
        if not doc or not slot:
            raise HTTPException(
                status_code=409,
                detail=f"No available slots for Dr. {doc.full_name} at this time."
            )

        specialist_id = doc.id

    # Compute slot
    today = datetime.today().date()
    start_time = slot
    end_time = (datetime.combine(today, slot) + timedelta(minutes=30)).time()

    new_appointment = Appointment(
        patient_name=data.patient_name,
        age=data.age,
        symptoms=data.symptoms,
        specialist=data.specialist,
        specialist_id=specialist_id,
        date=today,
        start_time=start_time,
        end_time=end_time,
        status="confirmed"
    )

    db.add(new_appointment)
    db.commit()
    db.refresh(new_appointment)

    return {
        "message": "Appointment booked successfully",
        "appointment": {
            "id": new_appointment.id,
            "patient_name": new_appointment.patient_name,
            "age": new_appointment.age,
            "symptoms": new_appointment.symptoms,
            "specialist": new_appointment.specialist,
            "doctor": doc.full_name,
            "date": str(today),
            "time": start_time.strftime('%I:%M %p')
        }
    }
    
from fastapi import Query
from datetime import datetime, date as date_type
from utils.slot_utils import compute_available_slots
@router.get("/doctors/{doctor_id}/slots")
def get_available_slots(

    doctor_id: int,

    date: str = Query(...),

    db: Session = Depends(get_db)):
    doctor = db.query(User).filter_by(id=doctor_id, role='specialist').first()
    if not doctor:
        raise HTTPException(status_code=404, detail="Doctor not found")

    try:
        appointment_date = datetime.strptime(date, "%Y-%m-%d").date()
    except ValueError:
        raise HTTPException(status_code=400, detail="Invalid date format")

    slots = compute_available_slots(doctor, appointment_date, db)
    return {"available_slots": slots}

from schemas.appointment import AppointmentAuto
from sqlalchemy import func
@router.post("/appointments/auto")
def create_auto_appointment(data: AppointmentAuto, db: Session = Depends(get_db)):
    # Step 1: Predict specialist
    print(data.dict())
    predicted = predict_specialist(data.symptoms)
    specialization = predicted[0] if isinstance(predicted, tuple) else predicted
    # Step 2: Fetch all doctors for predicted specialization
    
    doctors = db.query(User).filter(func.lower(User.specialization) == specialization.strip().lower()).all()
    if not doctors:
        raise HTTPException(
        status_code=404,
        detail=f"Doctor specialization '{specialization}' is not registered. Please try again later or choose another category.")
    # Step 3: Check slots for each doctor
    today = datetime.now().date()
    for doctor in doctors:
        for offset in range(0, 7):  # check next 7 days
            check_date = today + timedelta(days=offset)
            
            slots = compute_available_slots(doctor, check_date, db)
            print(slots)
            if slots:
                # Step 4: Pick earliest available slot
                print(slots)
                start_time = datetime.strptime(slots[0], "%H:%M").time()
                end_time = (datetime.combine(date_type.today(), start_time) + timedelta(minutes=30)).time()

                # Step 5: Book appointment
                appointment = Appointment(
                    patient_name=data.patient_name,
                    age=data.age,
                    symptoms=data.symptoms,
                    specialist=specialization,
                    specialist_id=doctor.id,
                    date=check_date,
                    start_time=start_time,
                    end_time=end_time,
                    status="confirmed"
                )
                db.add(appointment)
                db.commit()
                db.refresh(appointment)
                return {"message": "Appointment booked", "appointment": appointment}

    raise HTTPException(status_code=404, detail="No slots available in next 7 days")


@router.get("/appointments/all")
def get_all_appointments(db: Session = Depends(get_db)):
    appointments = db.query(Appointment).all()
    result = []
    for appt in appointments:
        result.append({
            "id": appt.id,
            "patient_name": appt.patient_name,
            "age": appt.age,
            "symptoms": appt.symptoms,
            "specialist": appt.specialist,
            "doctor": appt.doctor.full_name if appt.doctor else "N/A",
            "date": str(appt.date),
            "time": appt.start_time.strftime('%I:%M %p') if appt.start_time else "N/A",
            "status": appt.status,
        })
    return result


@router.put("/appointments/{appointment_id}/status")
def update_appointment_status(appointment_id: int, status: str, db: Session = Depends(get_db)):
    appointment = db.query(Appointment).filter(Appointment.id == appointment_id).first()
    if not appointment:
        raise HTTPException(status_code=404, detail="Appointment not found")

    appointment.status = status
    db.commit()
    db.refresh(appointment)
    return {"message": "Status updated", "appointment": appointment}

@router.get("/appointments/doctor/{doctor_id}")
def get_appointments_for_doctor(doctor_id: int, db: Session = Depends(get_db)):
    appointments = db.query(Appointment).filter(Appointment.specialist_id == doctor_id).all()
    if not appointments:
        raise HTTPException(status_code=404, detail="No appointments found for this doctor")
    return appointments