# Telemedicine Platform SPARQL Queries

PREFIX tele: <http://example.org/telemedicine#>
PREFIX schema: <http://schema.org/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

# Query 1: Find active video consultations
SELECT ?consultation ?provider ?patient ?startTime
WHERE {
    ?consultation a tele:VideoConsultation ;
        tele:hasProvider ?provider ;
        tele:hasPatient ?patient ;
        tele:hasStartTime ?startTime ;
        tele:hasStatus "in-progress" .
}
ORDER BY ?startTime

# Query 2: Get consultations by provider
SELECT ?consultation ?patientName ?type ?status
WHERE {
    ?consultation tele:hasProvider ?provider ;
        tele:hasPatient ?patient ;
        tele:hasConsultationType ?type ;
        tele:hasStatus ?status .
    ?patient schema:name ?patientName .
    FILTER(?provider = tele:provider_dr_smith)
}

# Query 3: Find prescriptions from consultations
SELECT ?prescription ?medication ?dosage ?pharmacy
WHERE {
    ?consultation a tele:Consultation ;
        tele:hasPrescription ?prescription .
    ?prescription tele:hasMedication ?medication ;
        tele:hasDosage ?dosage ;
        tele:sentToPharmacy ?pharmacy .
    ?medication schema:name ?medName .
}

# Query 4: Get upcoming appointments
SELECT ?appointment ?provider ?patient ?scheduledTime
WHERE {
    ?appointment a tele:Appointment ;
        tele:hasProvider ?provider ;
        tele:hasPatient ?patient ;
        tele:hasScheduledTime ?scheduledTime .
    FILTER(?scheduledTime > NOW())
}
ORDER BY ?scheduledTime

# Query 5: Find consultations with vital signs
SELECT ?consultation ?vitalType ?value ?unit
WHERE {
    ?consultation a tele:Consultation ;
        tele:hasVitalSign ?vital .
    ?vital a ?vitalType ;
        schema:value ?value ;
        schema:unitCode ?unit .
}

# Query 6: Get encrypted consultations
SELECT ?consultation ?provider ?patient ?encryptionKey
WHERE {
    ?consultation a tele:VideoConsultation ;
        tele:hasProvider ?provider ;
        tele:hasPatient ?patient ;
        tele:isEncrypted true ;
        tele:hasEncryptionKey ?encryptionKey .
}

# Query 7: Find patients with consent
SELECT ?patient ?name ?consentDate
WHERE {
    ?patient a tele:Patient ;
        schema:name ?name ;
        tele:hasConsentDate ?consentDate .
    FILTER(?consentDate < NOW())
}

# Query 8: Get consultation duration statistics
SELECT ?provider (AVG(?duration) AS ?avgDuration) (COUNT(?consultation) AS ?count)
WHERE {
    ?consultation a tele:Consultation ;
        tele:hasProvider ?provider ;
        tele:hasDuration ?duration .
}
GROUP BY ?provider

# Query 9: Find consultations by date range
SELECT ?consultation ?provider ?patient ?startTime ?status
WHERE {
    ?consultation a tele:Consultation ;
        tele:hasProvider ?provider ;
        tele:hasPatient ?patient ;
        tele:hasStartTime ?startTime ;
        tele:hasStatus ?status .
    FILTER(?startTime >= "2025-01-01T00:00:00Z"^^xsd:dateTime &&
           ?startTime <= "2025-01-31T23:59:59Z"^^xsd:dateTime)
}

# Query 10: Get patient's consultation history
SELECT ?consultation ?provider ?startTime ?diagnosis ?prescription
WHERE {
    ?consultation tele:hasPatient tele:patient_john_doe ;
        tele:hasProvider ?provider ;
        tele:hasStartTime ?startTime .
    OPTIONAL { ?consultation tele:hasDiagnosis ?diagnosis }
    OPTIONAL { ?consultation tele:hasPrescription ?prescription }
}
ORDER BY DESC(?startTime)

# Query 11: Find high-bandwidth video streams
SELECT ?consultation ?stream ?quality ?bandwidth
WHERE {
    ?consultation a tele:VideoConsultation ;
        tele:hasVideoStream ?stream .
    ?stream tele:hasVideoQuality ?quality ;
        tele:hasBandwidth ?bandwidth .
    FILTER(?bandwidth > 2000)
}

# Query 12: Get pharmacy prescription counts
SELECT ?pharmacy (COUNT(?prescription) AS ?prescriptionCount)
WHERE {
    ?prescription a tele:EPrescription ;
        tele:sentToPharmacy ?pharmacy .
}
GROUP BY ?pharmacy
ORDER BY DESC(?prescriptionCount)
