<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;


use App\Models;
use Session;
use Illuminate\Support\Arr;
use App\Jobs\SendMnoContractRenewalEmailAlert;
use App\Jobs\SendSupportFeesReminderEmailAlert;
use App\Http\Requests;
use Carbon\Carbon;

class ContractRenewalReminderController extends Controller
{
  public function getMnos(){
      $network_arr = Models\NetworkOps::with('account_manager_info')->where('contract_auto_renew', 'NO')->get();
      $renew_ready = [];
      $current_date = date('Y-m-d');
      foreach ($network_arr as $value) {
        if ($value->contract_validity == false) {
          continue;
        }
        $date1 = date_create($current_date);
        $date2 = date_create($value->contract_validity);
        $difference = date_diff($date1, $date2);
        if ($difference->days <= 45) {
          $renew_ready['mno_name'] = $value->name;
          $renew_ready['account_manager_email'] = $value->account_manager_info->email;
          $renew_ready['days_to_expire'] = $difference->days;
          dispatch(new SendMnoContractRenewalEmailAlert($renew_ready));
        }
      }
      $log_data = implode(', ', $renew_ready);
      \Log::info('MNOs due for renewal ' . $log_data);
      $this->sendNtfy('MNOs due for renewal ' . $log_data);
  }
  public function getSupportFeesList(){
      $support_fees_arr = Models\ClientSupportFees::with('created_by_info', 'client_info')->where('recurring', '<>', 'NO')->get();
      // dd($support_fees_arr);
      $renew_ready = [];
      $current_date = date('Y-m-d');
      foreach ($support_fees_arr as $value) {
        // dd($value->created_by_info);
        $date1 = date_create($current_date);
        switch ($value->recurring) {
          case 'Annual':
            $date2 = date('Y-m-d', strtotime('+1 year', strtotime($value->invoice_date)));
            $date2 = date_create($value->invoice_date);
            $difference = date_diff($date1, $date2);
            break;
          case 'Semiannual':
            $date2 = date('Y-m-d', strtotime('+6 months', strtotime($value->invoice_date)));
            $date2 = date_create($value->invoice_date);
            $difference = date_diff($date1, $date2);
            break;
          case 'Quarterly':
            $date2 = date('Y-m-d', strtotime('+3 months', strtotime($value->invoice_date)));
            $date2 = date_create($value->invoice_date);
            $difference = date_diff($date1, $date2);
            break;
          case 'Monthly':
            $date2 = date('Y-m-d', strtotime('+1 month', strtotime($value->invoice_date)));
            $date2 = date_create($date2);
            $difference = date_diff($date1, $date2);
            break;
          default:
            continue 2;
            break;
        }
      }
      if (isset($difference)) {
        //will review this later
        if ($difference->days <= 10) {
          $renew_ready['client_name'] = $value->client_info->name;
          $renew_ready['account_manager_email'] = $value->created_by_info->email;
          $renew_ready['days_to_renew'] = $difference->days;
          dispatch(new SendSupportFeesReminderEmailAlert($renew_ready));
        }
      }
      \Log::info('Support Fees Reminder schedule has successfully ran ');
      if (count($renew_ready) > 0) {
        $log_data = implode(', ', $renew_ready);
        \Log::info('Support Fees Reminder for ' . $log_data);
        $this->sendNtfy('Support Fees due for renewal ' . $log_data);
      }
  }
}
