<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models;
use Spatie\Activitylog\Models\Activity;
use Illuminate\Contracts\Mail\Mailer;
use App\Jobs\SendTestEmail;

class UtilityController extends Controller
{
    public function underconstruction(){
      return view('utility.underconstruction');
    }
    public function migrateSenderids(){
        $senderid = Models\Senderidold::with('network_info', 'client_info')->where('direct_mno', 'YES')->get();
        // dd($senderid);
        $counter = 0;
        foreach ($senderid as $row) {
            
            // dd($row);
            $senderid_arr['senderid'] = $row->senderid;
            $senderid_arr['created_by'] = $row->created_by;
            $senderid_arr['last_modified_by'] = $row->last_modified_by;
            $senderid_arr['remarks'] = $row->remarks;
            $senderid_arr['mno_name'] = $row->network_info->name . ' (' . $row->network_info->country .')';
            $senderid_arr['status'] = $row->status;
            $senderid_arr['direct_mno'] = $row->direct_mno;
            $senderid_arr['created_at'] = $row->created_at;
            $senderid_arr['modified_at'] = $row->modified_at;
            
            $result = Models\SenderId::create($senderid_arr);
            // dump($result);
            $counter++;
        }
        return ['gip' => 'string', 'counter' => $counter];
    }
    public function EmailTest(Mailer $mailer)
    {
         dispatch(new SendTestEmail());
        /*
        $emails = ['kwesi@click-mobile.com', 'kwesi_banson@hotmail.com'];

        $data = [
            'client' => 'test client',
            'created_by' => 'Kwesi',
            'services' => 'test services',
            'notes_body' => 'test notes'
        ];
        $mailer->send('emails.new-notes', $data, function ($message) use ($data, $emails) {
            $message->from('erp@click-mobile.com', 'Click Mobile ERP');
            $message->to($emails)->subject('New Notes');
        });
        */
        /*
        $emails = [
            'kwesi@click-mobile.com', 
            'kwesi_banson@hotmail.com'
        ];
        $data = [];
        $top = $mailer->send('emails.test', $data, function ($message) use ($data, $emails) {
            $message->from('support@click-mobile.com', 'Click Mobile ERP');
            $message->to($emails)->subject('Short Code Invoices');
        });

        dd($top);
        */
    }
    /**
     * maptest function to display the map view.
     *
     * @return Illuminate\View\View
     */
    public function maptest(){
        return view('utility.map');
    }

    public function showPage(){

        return view('utility.arraylogic');
    }
    public function ntfyTest(){
        $this->sendNtfy('In the ERP');
    }
    public function paperlessTest(){
      $file_path = "documents/oasl.pdf";
      $tags_arr = [1,2];
      $created_at = date('Y-m-d H:i:s');
      $document_type = 1;
      $title = "OASL has entered the chat";
      $top = $this->sendToPaperless($file_path, $tags_arr, $created_at, $document_type, $title);
      dd($top);
    }
    function getPaperlessAttributes($name){
      $retval = $this->getToPaperlessParamValues($name);
      // dd($retval['results']);
      $tip = json_decode($retval);
      dd($tip);
      foreach ($tip->results as $value) {
        dd($value);
      }
      dd($tip->results);

    }
    function insertOnboardingProgress(){
        $all_clients = Models\Client::get();
        $count_cl = 0;
        foreach ($all_clients as $row) {
            $get_stage_subs_items = Models\ClientOnboardingSubItem::get();
            foreach ($get_stage_subs_items as $value) {
                $stage_id = ['stage_id' => $value->stage_id, 'client_id' => $row->id, 'name' => $value->name ];
                $progress_arr = [
                    'status' => 'PENDING'
                ];
                $clients_onboarding_progress = Models\ClientOnboardingProgress::updateOrCreate($stage_id, $progress_arr);
            }
            $count_cl++;
        }
        dump($count_cl);
    }
    function massOnboardingProgress($client_id){
        //todo : update
        /*
        -- client_onboarding_sub_items -- this holds all sub items
        -- client_onboarding_progress -- update to completed
        -- in the clients table
            -- onboarding_progress_stage == update to complete,
            -- progress_indicator to complete,
            -- progress_indicator_score to 100
        */
        $get_stage_subs_items = Models\ClientOnboardingSubItem::get();
        foreach ($get_stage_subs_items as $value) {
            $progress_arr = [
                'stage_id' => $value->stage_id,
                'client_id' => $client_id,
                'name' => $value->name,
                'status' => 'COMPLETE'
            ];
            $clients_onboarding_progress = Models\ClientOnboardingProgress::create($progress_arr);
        }
        $all_clients = Models\Client::get();
        $count_cl = 0;
        foreach ($all_clients as $row) {
            $get_stage_subs_items = Models\ClientOnboardingSubItem::get();
            foreach ($get_stage_subs_items as $value) {
                $stage_id = ['stage_id' => $value->stage_id, 'client_id' => $row->id, 'name' => $value->name ];
                $progress_arr = [
                    'status' => 'PENDING'
                ];
                $clients_onboarding_progress = Models\ClientOnboardingProgress::updateOrCreate($stage_id, $progress_arr);
            }
            $count_cl++;
        }
        dump($count_cl);
    }
    function loggingTest(){
      $rando = uniqid();

      $newsItem = Models\Utility::create([
         'name' => 'original name : ' . $rando,
         'type' => 'Lorum'
      ]);
      $activity = Activity::all()->last();

      dump($activity->description); //returns 'created'
      dump($activity->subject); //returns the instance of NewsItem that was created
      dump($activity->changes); //returns ['attributes' => ['name' => 'original name', 'text' => 'Lorum']];
      //
      $rando = uniqid();

      $newsItem->name = 'updated name to something like this ' . $rando;
      $newsItem->type = 'franko';
      $newsItem->save();

      //updating the newsItem will cause an activity being logged
      $activity_up = Activity::all()->last();
      dump($activity_up);
      dump($activity_up->description); //returns 'updated'
      dump($activity_up->subject); //returns the instance of NewsItem that was created
      dump($activity_up->changes);
    }

    public function loadShortCodes(){
        $short_codes_file = public_path('misc/client_short_codes.csv');
        $codes_arr = file($short_codes_file);

        dump($codes_arr);
        // "﻿Friendly Name"
        // 1 => "Shortcode"
        // 2 => "Network"
        // 3 => "Client"
        // 4 => "Status"
          //       0 => "﻿Nexmo"
          // 1 => "40520"
          // 2 => "Safaricom"
          // 3 => "Nexmo"
          // 4 => "LIVE\r\n"

        $client_arr = [];
        foreach ($codes_arr as $row) {
            $row_arr = explode(',', $row);
            $client_arr['name'] = trim($row_arr[0]);
            $client_arr['shortcode'] = trim($row_arr[1]);
            $client_arr['network'] =  trim($row_arr[2]);
            $client_arr['client'] =  trim($row_arr[3]);
            $client_arr['status'] =  trim($row_arr[4]);
            
            dd($client_arr);
        }

    }

    public function getClient($name){

    }
    public function getNetwork($name){

    }
    public function processMnoWorldwideFile(){

        
        $file = public_path('helper_documents/mcc_mnc_table.json');
        
        $jsonData = file_get_contents($file);

        // Decode the JSON string into an associative array
        $dataArray = json_decode($jsonData, true);

        if (json_last_error() === JSON_ERROR_NONE) {
            $counter = 0;
            foreach ($dataArray as $key => $value) {
                $result = Models\Worldwidemno::create($value);
                $counter++;
            }
            dump($counter . " Records found and inserted");
        } else {
            dump('Error decoding JSON: ' . json_last_error_msg());
        }



        // dd($file);
        // $decoded = json_decode($file, true);
        // dd($decoded);

    }

}
// https://www.tokyvideo.com/video/sheena-the-queen-of-the-jungle-1984-movie-with-tanya-roberts-ted-wass-donovan-scott
