<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Support;
use App\Models\Support_Reply;
use Illuminate\Http\Request;
use App\Http\Requests\AddSupportRequest;

class SupportController extends Controller
{

    private $message;
    private $code;
    private $data;
    private $type;
    public function __construct()
    {
        $this->message = 'Can not save';
        $this->code = config('constant.API_FAILED_CODE');
        $this->data = [];
        $this->type = "error";
    }

    public function index(Request $request){

        $inputData = $request->all();
        $items = empty($request->pageLimit) ? config('constant.PAGINATION_LIMIT'):$request->pageLimit;

        $searchData=[
            'status'=> $request['status']??0,
            'search_value'=>$request['search_value'],
            'pageLimit' => $items,
            'user_type'=> auth()->user()->user_type,
            'user_id' => auth()->user()->id
        ];

        $supports =  (new Support())->list($searchData);

        return view('support.list', compact('supports','searchData'));

    }
    public function insertClientSupport(AddSupportRequest $request)
    {
        $action='ADD_SUPPORT_BY_CLIENT';
        $inputData = $request->all();
        $inputData['sender_id'] = auth()->user()->id;
        $inputData['message'] = $inputData['support_text'];
        $inputData['title'] = $inputData['support_title'];
        $inputData['status'] = 0;
        $return_data =  (new Support())->addSupport($inputData);
        if($return_data)
        {
            $this->type = "success";
            $this->message = 'Saved successfully';
        }

        return back()->with([$this->type => $this->message]);

    }

    public function add(AddSupportRequest $request)
    {
        $file_name = null;
        $inputData = $request->all();
        $inputData['sender_id'] = auth()->user()->id;
        $inputData['message'] = $inputData['support_text'];
        $inputData['title'] = $inputData['support_title'];
        $inputData['support_id'] = $inputData['id'];
        if (!empty($request->file('doc_file'))) {
            $file = $request->file('doc_file');
            $file_data = imageFileRotate($request->file('doc_file'));
            $file_name = time() . "_" . removeAnySpaces($file->getClientOriginalName(), '_');
            $file_path = 'support_ticket' . DIRECTORY_SEPARATOR . $inputData['id'];
            uploadFile($file_path, $file_name, $file_data);
        }
        $inputData['attachment_path'] = $file_name;
        $return_data = (new Support_Reply())->addSupportReply($inputData);
        if ($return_data) {
            $this->type = "success";
            $this->message = 'Saved successfully';
        }


        return back()->with([$this->type => $this->message]);

    }

    public function view($id){
        $inputData['sender_id'] = auth()->user()->id;
        $inputData['support_id'] = $id;

        (new Support_Reply())->updateSupportReply($inputData);
        $supports =  (new Support())->getSupportById($inputData);
        $support_data = (new Support())->getNotifcation($inputData['sender_id'],auth()->user()->user_type);
        setSession('new_ticket',$support_data);

        return view('support.view', compact('supports'));

    }
}
