<?php
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;

use App\Traits\CallLogTrait;

class FileController extends Controller
{
	use CallLogTrait;

	private $debug;
	private $validFileExtentions = [
		'mp3', 'amr', 'wav', 'mp4', 'm4a'
	];
	private $validFileSize = 1024 * 10;

	public function __construct(Request $request)
	{
		if($request->has('debug') && $request->input('debug') == true) {
			$this->debug = true;
		}
	}

	public function uploadCallRecord(Request $request)
	{
		$recored_file = '';
		if($request->hasFile('record_file')) {
			$file = $request->file('record_file');
			$extension = $file->getClientOriginalExtension();
			$size = $file->getSize();

			if(!in_array(strtolower($extension), $this->validFileExtentions)) {
				return new JsonResponse(
					array(
						"status#"		=>	"401",
						"msg#"			=> 	"Invalid file format!"
					)
				);
			}

			Storage::disk('local')->put($file->getFilename() . '.' . $extension,  File::get($file));

			$recored_file = $file->getFilename() . '.' . $extension;
		}

		$data = $request->all();
		$data['recored_file'] = $recored_file;

		$callLogId = $this->insertCallLog($data);

		return new JsonResponse(
			array(
				"status#"		=>	"1",
				"msg#"			=> 	"Record added successfully!",
				'call_log_id'	=>	$callLogId	
			)
		);
	}

}