<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;
use App\Models;
use Session;
use App\Libs\Smsgateway; 
use Carbon\Carbon;
use App\ActivityLog;
use Validator;

class UtilityController extends Controller
{
    public function index(){
        \Log::info('here at the wall : users view');
        //where('status','Active')->
        // $users = User::where('status','Active')->latest()->paginate(10);

        // $data = [

        //     'users' => $users,
        // ];
        
        // // 
        // return view('users.index',$data);
    }
    public function customLogExample()
    {
        Log::channel('custom')->info('This is a custom log message.');

        return response()->json(['message' => 'Custom log written successfully!']);
    }

    public function mattermosttest()
    {
        // log::info('This is a test message for Mattermost logging.');
        // logger()->error('Some message', [
        //     'context' => 'Some contex',
        //     'an_array_of_things' => ['foo', 'bar', 'baz'],
        // ]);
        Log::channel('mattermost')->info("mattermost log test message");

        // Or

        // throw new \Exception('An exception occured');
        Log::error('Something really important');


        // Log::channel('mattermost')->info('This is a test message for Mattermost logging.');
        return response()->json(['message' => 'Mattermost log written successfully!']);
    }
    public function refundStore(Request $request){   
        $validatedData = $request->validate([
            'dealer_id' => 'required',
            'merchant_id' => 'required',
            'amount' => 'required|numeric',
            'reason' => 'required',
        ]);
        $merchant = Models\Merchant::findorfail($request->merchant_id);
        $merchant_name = $merchant->fullname;
        $dealer = Models\OrgUser::findorfail($request->dealer_id);
        $dealer_name = $dealer->name;
        $organisation = Models\Organisation::findorfail($dealer->org_id);
        $result = Models\MerchantToDealerRefund::create([
            'dealer_id' => $request->dealer_id,
            'merchant_id' => $request->dealer_id,
            'amount' => $request->amount,
            'dealer_msisdn' => $dealer->phone,
            'reason' => $request->reason,
        ]);
        $merchant_balance = $merchant->balance;
        $merchant->balance = $merchant_balance - $request->amount;
        $merchant->save();

        //Review this -- should dealers belong to a larger organisation or they should be on their own
        $dealer_balance = $organisation->balance;
        $organisation->balance = $dealer_balance + $request->amount;
        $organisation->save();

        $desc = [
            'title' => 'Dealer Refund',
            'data' => "Redunded a dealer with an amount of  " . $request->amount,
            'type' => 'merchant',
        ];

        $activity = new ActivityLog;
        $activity->user_id = $request->session()->get('current_org_user.id'); // org_user_id ;
        $activity->user_type = 'merchant';
        $activity->description = json_encode($desc);
        $activity->save();
        

        $msg = "You have been Successfully refunded a dealer ($dealer_name) with an amount of $request->amount.";
        $dealer_msg = "You have received a refund amount of $request->amount from Airtime Merchant ($merchant_name)";
        $smsgateway = new Smsgateway; 
        if(env('APP_ENV') !== 'local'){
            $result = $smsgateway->sendSmsNew($merchant->phone, $msg);
            $result = $smsgateway->sendSmsNew($dealer->phone, $dealer_msg);
        }
        \Log::info($msg);
        \Log::info($dealer_msg);
        // $this->sendNtfy($msg);
        if($request->ajax()){
            return response()->json([
                'code' => 1,
                'status' => 'success',
                'message' => 'Dealer Refund Successfully processed'
            ]);
        }
        else{
            return redirect()->to('merchant/refunds')->with('success_message', 'Dealer Refund Successfully processed');
        }
    }
    
}
