<?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\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 ModulesGarden\HostingQuotaNotifications\App\Libs\Cron\Notifications\Queries\Interfaces\TypeQuery;


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

    public static function checkForMessages($conf, $products, $kind)
    {
        if ($conf['unit'] == GroupConstants::PERCENTAGE)
        {
            return self::checkByPercantage($conf['value'], $products, $kind, $conf['countedDays']);
        }
        return self::checkByValue($conf['value'], $products, $kind, $conf['countedDays']);
    }


    public static function checkByValue($value, $products, $kind, $days)
    {

        $productsTable = Tables::tableNames('product');
        $hostingTable  = Tables::tableNames('hosting');
        $usageTable    = Tables::tableNames('usage');


        return Usage::select($usageTable . '.hid', DB::raw('MAX(' . $usageTable . '.' . $kind . ') - MIN(' . $usageTable . '.' . $kind . ') AS '. GroupConstants::MB))
            ->join($hostingTable, $hostingTable . '.id', '=', $usageTable . '.hid')
            ->join($productsTable, $productsTable . '.id', '=', $hostingTable . '.packageid')
            ->whereIn($productsTable . '.id', $products)
            ->whereRaw($usageTable . '.date BETWEEN DATE_SUB(CURDATE(), INTERVAL ? DAY) AND CURDATE()', [$days])
            ->where($hostingTable . '.domainstatus', '=', 'Active')
            ->havingRaw('(MAX(' . $usageTable . '.' . $kind . ') - MIN(' . $usageTable . '.' . $kind . ')) > ?', [$value])
            ->groupBy($usageTable . '.hid')
            ->get()
            ->toArray();
    }

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

        return Usage::select($usageTable . '.hid', $hostingTable . '.' . $kind . 'limit AS customLimit',
            DB::raw('((MAX(' . $usageTable . '.' . $kind . ') - MIN(' . $usageTable . '.' . $kind . ')) / ' . $hostingTable . '.' . $kind . 'limit * 100) AS '.GroupConstants::PERCENTAGE))
            ->join($hostingTable, $hostingTable . '.id', '=', $usageTable . '.hid')
            ->join($productsTable, $productsTable . '.id', '=', $hostingTable . '.packageid')
            ->whereIn($productsTable . '.id', $products)
            ->whereRaw($usageTable . '.date BETWEEN DATE_SUB(CURDATE(), INTERVAL ? DAY) AND CURDATE()', [$days])
            ->where($hostingTable . '.' . $kind . 'limit', '!=', 0)
            ->where("{$hostingTable}.domainstatus", '=', 'Active')
            ->havingRaw('((MAX(' . $usageTable . '.' . $kind . ') - MIN(' . $usageTable . '.' . $kind . ')) / ' . $hostingTable . '.' . $kind . 'limit * 100) > ?', [$value])
            ->groupBy($usageTable . '.hid')
            ->get()
            ->toArray();
    }
}
