<?php
/**
 *ata manipulation for contact page
 */
class ContactModel{

    /**
     * get contact information based on user type
     * @return array list of contacts based on the user type
     */
    public function getContactInfo(){
        global $user;

        $userType = ucfirst($user->getUserType());
        $method = 'getContactFor'.$userType;
        $method = str_replace("_", "", $method);

        if(method_exists ($this, $method)){
            return $this->{$method}();
        }else{
            trigger_error("No contact for this user type!", E_USER_ERROR);
        }
    }

    /**
     * [getAddresses description]
     * @param  String $userType the type of the user
     * @return Array           list of addresses based on user type
     */
    public function getAddresses($userType){
        global $database, $user;
        $extraJoin = "";

        if($userType === USER_TYPES['BROKER']){
            $extraJoin .= "INNER JOIN ".TABLES['commercial_leads']." cl
                            ON cl.idUser=a.idUser";
        }

        if($userType === USER_TYPES['COMMERCIAL_LEAD']){
            $extraJoin .= "INNER JOIN
                            (SELECT
                                b.idUser
                            FROM ".TABLES['brokers']." b
                            UNION ALL
                            SELECT c.idUser
                            FROM ".TABLES['customers']." c
                            INNER JOIN ".TABLES['rel_commercial_lead_customers']." rclc
                                ON rclc.idCustomer=c.id
                            INNER JOIN ".TABLES['commercial_leads']." cl
                                ON cl.id=rclc.idCommercialLead
                            WHERE cl.idUser=".$user->getUserId()." AND rclc.isLinkEnabled=1
                            ) users
                            ON users.idUser=a.idUser";
        }

        if($userType === USER_TYPES['CUSTOMER']){
            $extraJoin .= "INNER JOIN ".TABLES['commercial_leads']." cl
                            ON cl.idUser=a.idUser
                            INNER JOIN ".TABLES['rel_commercial_lead_customers']." rclc
                                ON rclc.idCommercialLead=cl.id AND rclc.isLinkEnabled=1
                            INNER JOIN ".TABLES['customers']." cust
                                ON cust.id=rclc.idCustomer AND cust.idUser=".$user->getUserId();
        }

        $sql = "SELECT
                    a.idUser,
                    a.company,
                    a.firstName,
                    a.lastName,
                    a.city,
                    a.detailedAddress,
                    a.zip,
                    c.name as country
                FROM ".TABLES['billing_information']." a
                INNER JOIN ".TABLES['countries']." c
                ON c.id=a.idCountry
                $extraJoin";
        $data = [];
        $query = $database->query($sql);
        while($row=$database->fetchArray($query)){
            $data[$row['idUser']][] = $row;
        }

        return $data;
    }

    private function getSellCountries($userType){
        global $database, $user;

        $extraJoin = "";

        if($userType === USER_TYPES['CUSTOMER']){
            $extraJoin .= "INNER JOIN ".TABLES['rel_commercial_lead_customers']." rclc
                                ON rclc.idCommercialLead=plcl.idCommercialLead AND rclc.isLinkEnabled=1
                            INNER JOIN ".TABLES['customers']." cust
                                ON cust.id=rclc.idCustomer AND cust.idUser=".$user->getUserId();
        }

        if($userType === USER_TYPES['COMMERCIAL_LEAD']){
            $sql ="SELECT DISTINCT
                    c.name,
                    c.code,
                    0 AS idUserByType
                    FROM ".TABLES['countries']." c
                    INNER JOIN ".TABLES['packages']." p
                    ON p.idCountry=c.id
                    INNER JOIN ".TABLES['price_list_broker']." plb
                    ON plb.idPackage=p.id
                    ORDER BY c.name";
        }else{
            $sql ="SELECT DISTINCT
                    c.name,
                    c.code,
                    plcl.idCommercialLead AS idUserByType
                    FROM ".TABLES['countries']." c
                    INNER JOIN ".TABLES['packages']." p
                    ON p.idCountry=c.id
                    INNER JOIN ".TABLES['price_list_commercial_lead']." plcl
                    ON plcl.idPackage=p.id
                    $extraJoin
                    ORDER BY c.name";
        }

        $data = [];
        $query = $database->query($sql);
        while($row=$database->fetchArray($query)){
            $data[$row['idUserByType']][] = $row;
        }

        return $data;
    }

    /**
     * get contatc informations for brokers
     * @return array list of contacts for broker
     */
    public function getContactForBroker(){
        global $database, $user;

        $addresses = $this->getAddresses($user->getUserType());
        $countries = $this->getSellCountries($user->getUserType());

        $sql = "SELECT
                    cl.id as idCommercialLead,
                    cl.name,
                    cl.phone,
                    u.mail,
                    cl.idUser
                FROM ".TABLES['commercial_leads']." cl
                INNER JOIN ".TABLES['users']." u
                ON u.id=cl.idUser
                ORDER BY cl.name";

        $data = [];
        $query = $database->query($sql);
        while($row=$database->fetchArray($query)){
            $row['addresses'] = isset($addresses[$row['idUser']]) ? $addresses[$row['idUser']] : [];
            unset($row['idUser']);
            $row['countries'] = isset($countries[$row['idCommercialLead']]) ? $countries[$row['idCommercialLead']] : [];
            unset($row['idCommercialLead']);
            $data[] = $row;
        }

        return $data;
    }

    /**
     * get contact information for customers
     * @return array list of contacts for customers
     */
    public function getContactForCustomer(){
        global $database, $user;

        $addresses = $this->getAddresses($user->getUserType());
        $countries = $this->getSellCountries($user->getUserType());

        $sql = "SELECT
                    cl.id AS idCommercialLead,
                    cl.name,
                    cl.phone,
                    cl.mail,
                    cl.idUser,
                    cl.vatCode
                FROM ".TABLES['commercial_leads']." cl
                INNER JOIN ".TABLES['rel_commercial_lead_customers']." rclc
                    ON rclc.idCommercialLead=cl.id AND rclc.isLinkEnabled=1
                INNER JOIN ".TABLES['customers']." c
                    ON c.id=rclc.idCustomer
                WHERE c.idUser=".$user->getUserId()."
                ORDER BY cl.name";

        $data = [];
        $query = $database->query($sql);
        while($row=$database->fetchArray($query)){
            $row['addresses'] = isset($addresses[$row['idUser']]) ? $addresses[$row['idUser']] : [];
            unset($row['idUser']);
            $row['countries'] = isset($countries[$row['idCommercialLead']]) ? $countries[$row['idCommercialLead']] : [];
            unset($row['idCommercialLead']);
            $data[] = $row;
        }

        return $data;
    }

    /**
     * get contact informations for commercial lead
     * @return array list of contacts for commercial lead
     */
    public function getContactForCommerciallead(){
        global $database, $user;

        $addresses = $this->getAddresses($user->getUserType());
        $countries = $this->getSellCountries($user->getUserType());

        $sql = "SELECT
                    b.id AS idByUserType,
                    b.name,
                    b.phone,
                    u.mail,
                    b.idUser,
                    'brokers' AS type
                FROM ".TABLES['brokers']." b
                INNER JOIN ".TABLES['users']." u
                ON u.id=b.idUser
                UNION ALL
                SELECT
                    0 AS idByUserType,
                    c.name,
                    c.phone,
                    u.mail,
                    c.idUser,
                    'customers' AS type
                FROM ".TABLES['customers']." c
                INNER JOIN ".TABLES['rel_commercial_lead_customers']." rclc
                    ON rclc.idCustomer=c.id AND rclc.isLinkEnabled=1
                INNER JOIN ".TABLES['commercial_leads']." cl
                    ON cl.id=rclc.idCommercialLead
                INNER JOIN ".TABLES['users']." u
                    ON u.id=c.idUser
                WHERE cl.idUser=".$user->getUserId()."
                ORDER BY type,name";

        $data = [];
        $query = $database->query($sql);
        while($row=$database->fetchArray($query)){
            $row['addresses'] = isset($addresses[$row['idUser']]) ? $addresses[$row['idUser']] : [];
            unset($row['idUser']);
            if($row['idByUserType'] != 0){
                    $row['countries'] = isset($countries[0]) ? $countries[0] : [];
            }
            unset($row['idByUserType']);
            $data[$row['type']][] = $row;
        }

        return $data;
    }
}
