<?php

namespace ModulesGarden\HostingQuotaNotifications\App\Helper\SmartyHelper;

use ModulesGarden\HostingQuotaNotifications\Core\Helper;
use \Illuminate\Database\Capsule\Manager as DB;
use ModulesGarden\HostingQuotaNotifications\App\Helper\DBModelsHelper;

/**
 * Description of Affiliate
 *
 * @author Artur Pilch <artur.pi@modulesgarden.com>
 */
class Affiliate
{
    private $data;
    private $output;

    public function getUserID()
    {
        return $this->data['clientid'];
    }

    public function __construct($id)
    {

        $model = DB::table('tblaffiliates')
                ->select("tblaffiliates.*")
                ->where('id', $id)
                ->first();


        $this->data = (array) $model; //$db->raw("SELECT * FROM tblaffiliates WHERE id = ?", array($id))->first();
    }

    public function getFormatedData()
    {
        global $currency;
        $currency = getCurrency($userid);

        $this->output              = $this->data;
        $this->output['balance']   = formatCurrency($this->output['balance']);
        $this->output['withdrawn'] = formatCurrency($this->output['withdrawn']);
        return $this->output;
    }

    public function getMergeFields()
    {
        $map      = self::getMergeFieldsMap();
        $formated = $this->getFormatedData();
        $fields   = array();

        foreach ($map as $k => $v)
            $fields[$k] = $formated[$v];

        $fields['affiliate_referral_url'] = DBModelsHelper::getSystemURL() . ("/aff.php?aff=" . $this->data['id']);


        return $fields;
    }

    public static function getMergeFieldsNames()
    {
        return array_keys(self::getMergeFieldsMap());
    }

    private static function getMergeFieldsMap()
    {
        return [
            'affiliate_total_visits'    => 'visitors',
            'affiliate_balance'         => 'balance',
            'affiliate_withdrawn'       => 'withdrawn',
            'affiliate_referrals_table' => '',
            'affiliate_referral_url'    => '',
        ];
    }
}
