<?php

class Carpool {

    
    public function getCarpoolStatus($db, $gpsdb, $data) {
        
        $member_id = (int) $data['member_id'];
        if ($member_id < 1) {
            return NULL;
        }
        

        // get user email
        $q = "select username from members WHERE id=${member_id};";
        $r = pg_query($db, $q);
        if ($r && pg_num_rows($r)) {
            while ($f = pg_fetch_assoc($r)) {
                // get user email
                $user_email = "";
                // if not found return error, weird
            }
        }

        // get the sent invites
        $q = "select c.id, c.member_id, c.card_id, c.pool, c.status, c.added, c.updated, cf.firstname, cf.lastname, cf.email, cf.status, cf.accept_status, cf.pool_status
            from members_carpool c
            join members_carpool_friends cf on c.id = cf.carpool_id
            WHERE member_id = ${member_id};";

        $r = pg_query($db, $q);
        if ($r && pg_num_rows($r)) {
            while ($f = pg_fetch_assoc($r)) {
                // get user sent invites
                $sent_invites = array();
            }
        }

        // get the received invites
        $q = "select id, carpool_id,email, status, accept_status, pool_status, added, updated from members_carpool_friends where email = '${user_email}';";

        $r = pg_query($db, $q);
        if ($r && pg_num_rows($r)) {
            while ($f = pg_fetch_assoc($r)) {
                // get user received invites
                $received_invites = array();
            }
        }

        
        return array(
            'sent_invites' => $sent_invites,
            'received_invites' => $received_invites,
        );

    }

}