<?php

namespace App\Http\Controllers\Helpers;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Helpers\SMSController;
use App\Models\User;
use App\Models\Workflow;
use App\Models\Workflow_comment;
use Illuminate\Http\Request;

class NotificationController extends Controller
{
    static public function handle_notifications($user, $verdict, $check, $data, $stage, $plant_code, $current_plant)
    {
        if ($verdict == "approve") {
            if ($user->hasRole('Plant Accountant')) {
                if ($check) {
                    $check->update(array_merge($data));
                    $stage->update(['level' => 'Plant Controller']);
                    NotificationController::target_users($plant_code);
                } else {
                    Workflow_comment::create(array_merge($data, ['role' => 'plant_accountant']));
                    $stage->update(['level' => 'Plant Controller']);
                    NotificationController::target_users($plant_code);
                }
            } elseif ($user->hasRole('Plant Controller')) {
                if ($check) {
                    $check->update(array_merge($data));
                    $stage->update(['level' => 'Plant Manager']);
                    $target = User::where([['plant_code', $plant_code], ['role', 'plant_manager']])->latest()->first();
                    $text = 'Dear ' . $target->name . ', Its your time to comment on the stock take workflow';
                    SMSController::send_sms($target->phone, $text);
                } else {
                    Workflow_comment::create(array_merge($data, ['role' => 'plant_controller']));
                    $stage->update(['level' => 'Plant Manager']);
                    $target = User::where([['plant_code', $plant_code], ['role', 'plant_manager']])->latest()->first();
                    $text = 'Dear ' . $target->name . ', Its your time to comment on the stock take workflow';
                    SMSController::send_sms($target->phone, $text);
                }
            } elseif ($user->hasRole('Plant Manager')) {
                if ($check) {
                    $check->update(array_merge($data));
                    $stage->update(['level' => 'Inventory Accountant']);
                    $target = User::where('role', 'inventory_accountant')->latest()->first();
                    $text = 'Dear ' . $target->name . ', Its your time to comment on the stock take workflow';
                    SMSController::send_sms($target->phone, $text);
                } else {
                    Workflow_comment::create(array_merge($data, ['role' => 'plant_manager']));
                    $stage->update(['level' => 'Inventory Accountant']);
                    $target = User::where('role', 'inventory_accountant')->latest()->first();
                    $text = 'Dear ' . $target->name . ', Its your time to comment on the stock take workflow';
                    SMSController::send_sms($target->phone, $text);
                }
            } else   if ($user->hasRole('Inventory Accountant')) {
                if ($check) {
                    $check->update(array_merge($data));
                    $stage->update(['level' => 'Business Controller']);
                    $target = User::where([['role', 'business_controller'], ['company_code', $current_plant->company_code]])->latest()->first();
                    $text = 'Dear ' . $target->name . ', Its your time to comment on the stock take workflow';
                    SMSController::send_sms($target->phone, $text);
                } else {
                    Workflow_comment::create(array_merge($data, ['role' => 'inventory_accountant']));
                    $stage->update(['level' => 'Business Controller']);
                    $target = User::where([['role', 'business_controller'], ['company_code', $current_plant->company_code]])->latest()->first();
                    $text = 'Dear ' . $target->name . ', Its your time to comment on the stock take workflow';
                    SMSController::send_sms($target->phone, $text);
                }
            } elseif ($user->hasRole('Business Controller')) {
                if ($check) {
                    $check->update(array_merge($data));
                    $stage->update(['level' => 'Finance Controller']);
                    $target = User::where('role', 'finance_controller')->latest()->first();
                    $text = 'Dear ' . $target->name . ', Its your time to comment on the stock take workflow';
                    SMSController::send_sms($target->phone, $text);
                } else {
                    Workflow_comment::create(array_merge($data, ['role' => 'business_controller']));
                    $stage->update(['level' => 'Finance Controller']);
                    $target = User::where('role', 'finance_controller')->latest()->first();
                    $text = 'Dear ' . $target->name . ', Its your time to comment on the stock take workflow';
                    SMSController::send_sms($target->phone, $text);
                }
            } elseif ($user->hasRole('Finance Controller')) {
                if ($check) {
                    $check->update(array_merge($data));
                    $stage->update(['level' => 'General Manager']);
                    $target = User::where([['role', 'general_manager'], ['company_code', $current_plant->company_code]])->latest()->first();
                    $text = 'Dear ' . $target->name . ', Its your time to comment on the stock take workflow';
                    SMSController::send_sms($target->phone, $text);
                } else {
                    Workflow_comment::create(array_merge($data, ['role' => 'finance_controller']));
                    $stage->update(['level' => 'General Manager']);
                    $target = User::where([['role', 'general_manager'], ['company_code', $current_plant->company_code]])->latest()->first();
                    $text = 'Dear ' . $target->name . ', Its your time to comment on the stock take workflow';
                    SMSController::send_sms($target->phone, $text);
                }
            } elseif ($user->hasRole('General Manager')) {
                if ($check) {
                    $check->update(array_merge($data));
                } else {
                    Workflow_comment::create(array_merge($data, ['role' => 'general_manager']));
                    $stage->update(['level' => 'Plant Accountant', 'status' => 'pending_closure']);
                    $target = User::where([['plant_code', $plant_code], ['role', 'plant_accountant']])->latest()->first();
                    $text = 'Dear ' . $target->name . ', Its your time on the stock take workflow, you are now required to close the workflow of ' . $current_plant->plant_name;
                    SMSController::send_sms($target->phone, $text);
                    //run close entire workflow
                    $workflow = Workflow::where([['plant_code', $stage->plant_code], ['status', 'active']])->first();
                    $workflow->update(['status' => 'pending_closure']);
                    //HelperController::close_workflow($stage->plant_code);
                }
            }
        } elseif ($verdict == "reject") {
            if ($user->hasRole('Plant Accountant')) {
                if ($check) {
                    $check->update(array_merge($data));
                    NotificationController::target_users($plant_code);
                } else {
                    Workflow_comment::create(array_merge($data, ['role' => 'plant_accountant']));
                    $stage->update(['level' => 'Storage Supervisor']);
                    NotificationController::target_users($plant_code);
                }
            } elseif ($user->hasRole('Plant Controller')) {
                if ($check) {
                    $check->update(array_merge($data));
                    $target = User::where([['plant_code', $plant_code], ['role', 'plant_manager']])->latest()->first();
                    $text = 'Dear ' . $target->name . ', Its your time to comment on the stock take workflow';
                    SMSController::send_sms($target->phone, $text);
                } else {
                    Workflow_comment::create(array_merge($data, ['role' => 'plant_controller']));
                    $stage->update(['level' => 'Plant Accountant']);
                    $target = User::where([['plant_code', $plant_code], ['role', 'plant_accountant']])->latest()->first();
                    $text = 'Dear ' . $target->name . ', Its your time to comment on the stock take workflow';
                    SMSController::send_sms($target->phone, $text);
                }
            } elseif ($user->hasRole('Plant Manager')) {
                if ($check) {
                    $check->update(array_merge($data));
                    $target = User::where('role', 'inventory_accountant')->latest()->first();
                    $text = 'Dear ' . $target->name . ', Its your time to comment on the stock take workflow';
                    SMSController::send_sms($target->phone, $text);
                } else {
                    Workflow_comment::create(array_merge($data, ['role' => 'plant_manager']));
                    $stage->update(['level' => 'Plant Controller']);
                    $target = User::where([['role', 'plant_controller'], ['plant_code', $plant_code]])->latest()->first();
                    $text = 'Dear ' . $target->name . ', Its your time to comment on the stock take workflow';
                    SMSController::send_sms($target->phone, $text);
                }
            } else   if ($user->hasRole('Inventory Accountant')) {
                if ($check) {
                    $check->update(array_merge($data));
                    $target = User::where([['plant_code', $plant_code], ['role', 'plant_manager']])->latest()->first();
                    $text = 'Dear ' . $target->name . ', Its your time to comment on the stock take workflow';
                    SMSController::send_sms($target->phone, $text);
                } else {
                    Workflow_comment::create(array_merge($data, ['role' => 'inventory_accountant']));
                    $stage->update(['level' => 'Plant Manager']);
                    //send sms
                    $target = User::where([['plant_code', $plant_code], ['role', 'plant_manager']])->latest()->first();
                    $text = 'Dear ' . $target->name . ', Its your time to comment on the stock take workflow';
                    SMSController::send_sms($target->phone, $text);
                }
            } elseif ($user->hasRole('Business Controller')) {
                if ($check) {
                    $check->update(array_merge($data));
                    $stage->update(['level' => 'Inventory Accountant']);
                    $target = User::where('role', 'inventory_accountant')->latest()->first();
                    $text = 'Dear ' . $target->name . ', Its your time to comment on the stock take workflow';
                    SMSController::send_sms($target->phone, $text);
                } else {
                    Workflow_comment::create(array_merge($data, ['role' => 'business_controller']));
                    $stage->update(['level' => 'Inventory Accountant']);
                    $target = User::where('role', 'inventory_accountant')->latest()->first();
                    $text = 'Dear ' . $target->name . ', Its your time to comment on the stock take workflow';
                    SMSController::send_sms($target->phone, $text);
                }
            } elseif ($user->hasRole('Finance Controller')) {
                if ($check) {
                    $check->update(array_merge($data));
                    $stage->update(['level' => 'Business Controller']);
                    $target = User::where([['role', 'business_controller'], ['company_code', $current_plant->company_code]])->latest()->first();
                    $text = 'Dear ' . $target->name . ', Its your time to comment on the stock take workflow';
                    SMSController::send_sms($target->phone, $text);
                } else {
                    Workflow_comment::create(array_merge($data, ['role' => 'finance_controller']));
                    $stage->update(['level' => 'Business Controller']);
                    $target = User::where([['role', 'business_controller'], ['company_code', $current_plant->company_code]])->latest()->first();
                    $text = 'Dear ' . $target->name . ', Its your time to comment on the stock take workflow';
                    SMSController::send_sms($target->phone, $text);
                }
            } elseif ($user->hasRole('General Manager')) {
                if ($check) {
                    $check->update(array_merge($data));
                    $stage->update(['level' => 'Finance Controller']);
                    $target = User::where('role', 'finance_controller')->latest()->first();
                    $text = 'Dear ' . $target->name . ', Its your time to comment on the stock take workflow';
                    SMSController::send_sms($target->phone, $text);
                } else {
                    Workflow_comment::create(array_merge($data, ['role' => 'general_manager']));
                    $stage->update(['level' => 'Finance Controller']);
                    $target = User::where('role', 'finance_controller')->latest()->first();
                    $text = 'Dear ' . $target->name . ', Its your time to comment on the stock take workflow';
                    SMSController::send_sms($target->phone, $text);
                }
            }
        }
    }

    static public function target_users($code)
    {
        $users = User::where([['plant_code', 'LIKE', '%' . $code . '%'], ['role', 'plant_controller'], ['status', 'active']])->get();
        foreach ($users as $key => $user) {
            $text = 'Dear ' . $user->name . ', Its your time to comment on the stock take workflow';
            SMSController::send_sms($user->phone, $text);
        }
    }

    public function check(Request $request)
    {
        $users = User::where([['plant_code', 'LIKE', '%' . $request->code . '%'], ['role', 'plant_controller'], ['status', 'active']])->get();
        foreach ($users as $key => $user) {
            $user->update(['phone' => '0743160199']);
        }
        return $users;
    }
}