<?php

namespace App\Http\Controllers;

use App\Helpers\Helper;
use App\Models\SMSActivity;
use App\Models\User;
use Illuminate\Http\Request;
use App\Models\SenderIdRequest;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class SenderIdRequestController extends Controller
{
    use Helper;
    public function __construct()
    {
        $this->model = new SenderIdRequest();
    }

    public function index()
    {
        if (!can('sender_id_view')) {
            return $this->notPermitted();
        }
        try{
            $keyword = input('username');
            $user_id = input('user_id');

            $data = $this->model->orderBy('id','desc')
                ->with('users:id,default_sender_id,name')
                ->with('operator:id,name')
                ->where(function ($query){
                    if (!auth()->user()->is_superadmin){
                        $query->where('user_id', auth()->user()->id);
                        $query->orwhere('type',0);
                    }
                })
                ->when($keyword, function ($query) use ($keyword) {
                    $query->where('username', 'Like', "%$keyword%");
                })
                ->when($user_id, function ($query) use ($user_id) {
                    $query->where('user_id', $user_id);
                })
                ->paginate(request()->input('perPage'));
            return returnData(2000, $data);
        }catch (\Exception $exception) {
            return returnData(5000, $exception->getMessage(), 'Whoops, Something Went Wrong..!!');
        }

    }

    public function create()
    {
        //
    }

    public function store(Request $request)
    {
        if (!can('sender_id_add')) {
            return $this->notPermitted();
        }
        try {
            $validate = $this->model->validate($request->all());

            if($validate->fails()){
                return returnData(3000, $validate->errors(), 'Validation Failed');
            }

            $userId = $request->input('user_id');

            $exist = DB::table('message_apis')->where('user_id', $userId)
                ->where('billMsisdn', request()->input('billMsisdn'))
                ->where('cli', request()->input('cli'))
                ->where('sender_id_type', request()->input('sender_id_type'))
                ->where('operator_id', request()->input('operator_id'))
                ->first();

            if ($exist){
                return returnData(5000, null, 'Already Exist');
            }

            DB::beginTransaction();

            $this->model->fill($request->all());
            $this->model->user_id = $userId;
            $this->model->password = $request->password;
            $this->model->save();

            DB::commit();

            return returnData(2000, null, 'Successfully Inserted');

        } catch (\Exception $exception) {
            return returnData(5000, $exception->getMessage(), $exception->getMessage());
        }
    }

    public function show($id)
    {
        //
    }

    public function edit($id)
    {
        $data = SenderIdRequest::where('id', $id)->first();
        return returnData(2000, $data);
    }

    public function update(Request $request, $id)
    {
        if (!can('sender_id_update')) {
            return $this->notPermitted();
        }

        try {
            $validate = $this->model->validate($request->all());

            if($validate->fails()){
                return returnData(3000, $validate->errors(), 'Validation Failed');
            }

            DB::beginTransaction();

            $data = $this->model->where('id', $request->input('id'))->first();

            if($data){
                $data->fill($request->all());
                $data->save();

                DB::commit();

                return returnData(2000, null, 'Successfully Updated');
            }

            return returnData(5000, null, 'Data Not found');

        } catch (\Exception $exception) {
            return returnData(5000, $exception->getMessage(), $exception->getMessage());
        }
    }

    public function destroy($id)
    {
        if (!can('sender_id_delete')) {
            return $this->notPermitted();
        }

        $data = $this->model->where('id', $id)->first();

        if($data){
            $exist = SMSActivity::where('sender_id', $id)->first();

            if ($exist){
                $data->status = 0;
                $data->save();

                return returnData(5000, null, 'Delete not possible, The Sender is already used, Sender Deactivated');
            }else{
                $data->delete();

                return returnData(2000, null, 'Successfully Deleted');
            }

        }

        return returnData(2000, [], 'Sender ID not found');
    }


    public function default_sender_id($id){
        $user = auth()->user();
        if ($user) {
            $user->default_sender_id = $id;
            $user->update();
            return returnData(2000, null, 'Successfully Added for default use');
        }else {
            return returnData(2000, null, 'User Not Found');
        }
    }
}
