<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Jenssegers\Agent\Agent;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Mail;
use App\Http\Traits\StringGeneratorTrait;
use Ramsey\Uuid\Uuid;
use App\Mail\NotifyEmail;
use App\Models\WebForm;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests, StringGeneratorTrait;

    public function sendWebsiteEmailNotification($details){  
        if(config('app.env') == 'production'){
            $toUsers = [
            ['email' => config('app.supportEmailAddress'), 'name' => 'Hemband Support'],
            ['email' => config('app.adminEmailAddress'), 'name' => 'Hemband Admin'],
            ['email' => 'salshah497@gmail.com', 'name' => 'Sal Shah'],
            ['email' => 'hujaberi@gmail.com', 'name' => 'Hujaberi'],
            ['email' => 'shawn.shah@hemband.com', 'name' => 'Shawn Shah'],
            ['email' => 'robert.williams@hemband.com', 'name' => 'Robert Williams'],
            ['email' => 'nick.holden@hemband.com', 'name' => 'Nick Holden'],
            ['email' => 'adam.thomas@hemband.com', 'name' => 'Adam Thomas'],
            

        ];
        }else{
            $toUsers = [
                ['email' => config('app.testEmailAddress'), 'name' => 'Test Email Address']
            ];
        }
            
        Mail::to($toUsers)->send(new NotifyEmail($details));
        return true;
    }

    public function saveWebForm(Request $request){
        $data = $request->except(['form_name', '_token', 'g-recaptcha-response']);
        $agent = new Agent();
        $record = new WebForm;
        $record->iid = $this->makeIID();
        $record->uid = Uuid::uuid6();
        $record->form_name = $request->get('form_name');
        $record->ip_address = $request->ip();
        $record->device_type = $agent->device();
        $record->form_data = json_encode($data);

        $record->save();
        return $record;
    }
}
