<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace ModulesGarden\HostingQuotaNotifications\App\Libs\Cron\Notifications\Queries;

use ModulesGarden\HostingQuotaNotifications\App\Libs\Cron\Notifications\Queries\Interfaces\TypeQuery;
use \ModulesGarden\HostingQuotaNotifications\App\Models\Usage;
use \ModulesGarden\HostingQuotaNotifications\App\Helper\Tables;
use ModulesGarden\HostingQuotaNotifications\App\Libs\Groups\Constants as GroupConstants;
use \Illuminate\Database\Capsule\Manager as DB;
use function ModulesGarden\HostingQuotaNotifications\Core\Helper\redirectByUrl;

/**
 * Description of Tiered
 *
 * @author Artur Pilch <artur.pi@modulesgarden.com>
 */
class Tiered implements TypeQuery
{

    public static function checkForMessages($value, $products, $key)
    {
        $services = [];

        if(isset($value[GroupConstants::MB]))
        {
            $services[GroupConstants::MB] = self::checkByValue($value, $products, $key);
        }
        if(isset($value[GroupConstants::PERCENTAGE]))
        {
            $services[GroupConstants::PERCENTAGE] =  self::checkByPercentage($value, $products, $key);
        }
        return $services;
    }

    public static function checkByValue($value, $products, $key)
    {
        $productsTable = Tables::tableNames('product');
        $hostingTable  = Tables::tableNames('hosting');
        $usageTable    = Tables::tableNames('usage');


        return Usage::select($usageTable . '.hid', $usageTable . '.' . $key, $hostingTable . '.' . $key . 'limit')
            ->join($hostingTable, $hostingTable . '.id', '=', $usageTable . '.hid')
            ->join($productsTable, $productsTable . '.id', '=', $hostingTable . '.packageid')
            ->whereIn($productsTable . '.id', $products)
            ->whereRaw($usageTable . '.date = CURDATE()')
            ->where("{$hostingTable}.domainstatus", '=', 'Active')
            ->where($usageTable . '.' . $key, '>', $value[GroupConstants::MB])
            ->distinct()
            ->get()
            ->keyBy('hid')
            ->toArray();
    }

    public static function checkByPercentage($value, $products, $key)
    {
        $productsTable = Tables::tableNames('product');
        $hostingTable  = Tables::tableNames('hosting');
        $usageTable    = Tables::tableNames('usage');

        return Usage::select($usageTable . '.hid', $usageTable . '.' . $key, $hostingTable . '.' . $key . 'limit',
                DB::raw($hostingTable . '.' . $key . 'usage / ' . $hostingTable . '.' . $key . 'limit * 100 AS ' . GroupConstants::PERCENTAGE)
            )
            ->join($hostingTable, $hostingTable . '.id', '=', $usageTable . '.hid')
            ->join($productsTable, $productsTable . '.id', '=', $hostingTable . '.packageid')
            ->whereIn($productsTable . '.id', $products)
            ->whereRaw($usageTable . '.date = CURDATE()')
            ->where("{$hostingTable}.domainstatus", '=', 'Active')
            ->whereRaw($usageTable . '.' . $key . ' / ' . $hostingTable . '.' . $key . 'limit * 100 > ?', [$value[GroupConstants::PERCENTAGE]])
            ->distinct()
            ->get()
            ->keyBy('hid')
            ->toArray();
    }
}
