<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\CompanyMeta;

class ChatSettingController extends Controller
{
    public function chatSetting()
    {
        $display_chats = CompanyMeta::where('key', 'Display Chat')->where('type', 'Chat Setting')->get();
        $hide_chats = CompanyMeta::where('key', 'Hide Chat')->where('type', 'Chat Setting')->first();
        $canned_responses = CompanyMeta::where('key', 'Chat Canned Responses')->where('type', 'Chat Setting')->get();
        return view('chat-setting', ['display_chats' => $display_chats, 'hide_chats' => $hide_chats, 'canned_responses' => $canned_responses]);
    }
    
    public function storeFlowSetting(Request $request)
    {
        $companyId = getSelectedCompany();
        //Update Company Meta
        $flow_setting = [
            'show_profile_image_of_editors' => $request->show_profile_image_of_editors,
            'allow_visitor_to_send_messages' => $request->allow_visitor_to_send_messages,
            'max_number_of_chats_per_editor' => $request->max_number_of_chats_per_editor,
            'save_email_address_for_anonymous_chat' => $request->save_email_address_for_anonymous_chat,
            'delete_chat_data_automatically' => $request->delete_chat_data_automatically,
            'delay_for_automatic_deletion' => $request->delay_for_automatic_deletion,
            'allow_users_to_join_queue' => $request->allow_users_to_join_queue,
            'show_chat_button' => $request->show_chat_button,
            'visitor_can_send_images' => $request->visitor_can_send_images,
            'guest_must_write_name_and_email_to_chat' => $request->guest_must_write_name_and_email_to_chat,
            'logout_editor_who_missed_chat' => $request->logout_editor_who_missed_chat,
            'logout_everyone_automatically' => $request->logout_everyone_automatically,
            'chat_assistant_show_suggestion_form' => $request->chat_assistant_show_suggestion_form,
            'message_sent_to' => $request->message_sent_to,
        ];
        
        foreach($flow_setting as $key => $value) {
            if(!is_null($value)) {
                CompanyMeta::updateOrCreate([
                    'key' => $key,
                    'company_id' => $companyId,
                ],[
                    'company_id' => $companyId,
                    'key' => $key,
                    'value' => $value,
                    'type' => 'Chat Setting'
                ]);
            } else {
                CompanyMeta::where('key', $key)->where('company_id', $companyId)->where('type', 'Chat Setting')->delete();
            }
        }

        return redirect()->back()->with('success', 'Flow Setting Updated Successfully');
    }
    
    public function storeDisplayChat(Request $request)
    {
        $this->validate($request, [
            'path' => 'required',
            'start_chat_after' => 'required'
        ]);
    
        $companyId = getSelectedCompany();
        
        // Collect data into an array
        $display_data = [
            'path' => $request->path,
            'start_chat_after' => $request->start_chat_after,
        ];
        
        CompanyMeta::create([
            'company_id' => $companyId,
            'key' => 'Display Chat',
            'value' =>json_encode($display_data),
            'type' => 'Chat Setting'
        ]);
        
        return redirect()->back()->with('success', 'Chat Setting Updated Successfully');
    }
    
    public function deleteDisplayChat($id)
    {
        $display_chat = CompanyMeta::find($id);
        $display_chat->delete();
        
        return redirect()->back()->with('success', 'Chat Setting Updated Successfully');
    }
    
    public function storeHideChat(Request $request)
    {
        $this->validate($request, [
            'hide_chat_path' => 'required',
        ]);
    
        $companyId = getSelectedCompany();
        
        CompanyMeta::updateOrCreate([
            'company_id' => $companyId,
            'key' => 'Hide Chat',
        ],[
            'company_id' => $companyId,
            'key' => 'Hide Chat',
            'value' => $request->hide_chat_path,
            'type' => 'Chat Setting'
        ]);
        
        return redirect()->back()->with('success', 'Chat Setting Updated Successfully');
    }
    
    public function storeText(Request $request)
    {
        $companyId = getSelectedCompany();
        
        //Update Company Meta
        $text_data = [
            'public_name' => $request->public_name,
            'internal_name' => $request->internal_name,
            'wellcome_text' => $request->wellcome_text,
            'wellcome_text_at_queue' => $request->wellcome_text_at_queue,
            'start_message' => $request->start_message,
            'text_in_message_box' => $request->text_in_message_box,
            'message_when_chat_is_closed' => $request->message_when_chat_is_closed
        ];
        
        foreach($text_data as $key => $value) {
            if(!is_null($value)) {
                CompanyMeta::updateOrCreate([
                    'key' => $key,
                    'company_id' => $companyId,
                ],[
                    'company_id' => $companyId,
                    'key' => $key,
                    'value' => $value,
                    'type' => 'Chat Setting'
                ]);
            } else {
                CompanyMeta::where('key', $key)->where('company_id', $companyId)->where('type', 'Chat Setting')->delete();
            }
        }
        
        return response()->json(['success' => 'Text Setting Updated Successfully']);
    }
    
    public function storeStyle(Request $request)
    {
        $companyId = getSelectedCompany();
        
        //Update Company Meta
        $style_data = [
            'text_theme_color' => $request->text_theme_color,
            'background_theme_color' => $request->background_theme_color,
            'text_color_for_sent_message' => $request->text_color_for_sent_message,
            'background_color_of_sent_message' => $request->background_color_of_sent_message,
            'background_color_of_received_message' => $request->background_color_of_received_message,
            'text_color_of_received_message' => $request->text_color_of_received_message,
            'text_color_of_notification' => $request->text_color_of_notification,
            'text_color_of_error_message' => $request->text_color_of_error_message,
            'background_color_of_error_message' => $request->background_color_of_error_message,
            'link_color' => $request->link_color
        ];
        
        foreach($style_data as $key => $value) {
            if(!is_null($value)) {
                CompanyMeta::updateOrCreate([
                    'key' => $key,
                    'company_id' => $companyId,
                ],[
                    'company_id' => $companyId,
                    'key' => $key,
                    'value' => $value,
                    'type' => 'Chat Setting'
                ]);
            } else {
                CompanyMeta::where('key', $key)->where('company_id', $companyId)->where('type', 'Chat Setting')->delete();
            }
        }
        
        return redirect()->back()->with('success', 'Style Setting Updated Successfully');
    }
    
    public function storeChatCannedResponses(Request $request)
    {
        $this->validate($request, [
            'name' => 'required',
            'text' => 'required'
        ]);
    
        $companyId = getSelectedCompany();
        
        // Collect data into an array
        $canned_data = [
            'name' => $request->name,
            'text' => $request->text,
        ];
            
        // Encode the data array as JSON
        $jsonData = json_encode($canned_data);
        
        CompanyMeta::create([
            'company_id' => $companyId,
            'key' => 'Chat Canned Responses',
            'value' => $jsonData,
            'type' => 'Chat Setting'
        ]);

        return redirect()->back()->with('success', 'Canned Response Setting Updated Successfully');
    }
    
    public function deleteChatCannedResponses($id)
    {
        $get_chat_canned_response = CompanyMeta::find($id);
        $get_chat_canned_response->delete();
        
        return redirect()->back()->with('success', 'Canned Response Deleted Successfully.');
    }
    
    public function storePersonalData(Request $request)
    {
        $companyId = getSelectedCompany();
        
        //Update Company Meta
        $personal_data = [
            'name' => $request->name,
            'link_text' => $request->link_text,
            'active_approval' => $request->active_approval,
        ];
        
        foreach($personal_data as $key => $value) {
            if(!is_null($value)) {
                CompanyMeta::updateOrCreate([
                    'key' => $key,
                    'company_id' => $companyId,
                ],[
                    'company_id' => $companyId,
                    'key' => $key,
                    'value' => $value,
                    'type' => 'Chat Setting'
                ]);
            } else {
                CompanyMeta::where('key', $key)->where('company_id', $companyId)->where('type', 'Chat Setting')->delete();
            }
        }
        
        return redirect()->back()->with('success', 'Personal Data Updated Successfully');
    }
    
    public function storeTags(Request $request)
    {
        $companyId = getSelectedCompany();
        //Update Company Meta
        $tags_data = [
            'new_tags_to_be_created_when_tagging' => $request->new_tags_to_be_created_when_tagging,
        ];
        
        foreach($tags_data as $key => $value) {
            if(!is_null($value)) {
                CompanyMeta::updateOrCreate([
                    'key' => $key,
                    'company_id' => $companyId,
                ],[
                    'company_id' => $companyId,
                    'key' => $key,
                    'value' => $value,
                    'type' => 'Chat Setting'
                ]);
            } else {
                CompanyMeta::where('key', $key)->where('company_id', $companyId)->where('type', 'Chat Setting')->delete();
            }
        }
        
        return redirect()->back()->with('success', 'Tags Updated Successfully');
    }
    
    public function settingAllChat(Request $request)
    {
        $this->validate($request, [
            'heading_for_chat_flow' => 'required'
        ]);
        
        $companyId = getSelectedCompany();
        CompanyMeta::updateOrCreate([
            'key' => 'heading_for_chat_flow',
            'company_id' => $companyId,
        ],[
            'company_id' => $companyId,
            'key' => 'heading_for_chat_flow',
            'value' => $request->heading_for_chat_flow,
            'type' => 'Chat Setting'
        ]);
        return redirect()->back()->with('success', 'Chat Setting Updated Successfully');
    }
    
    public function blockIpAdresses(Request $request)
    {
        $this->validate($request, [
            'ip_addresses' => 'required'
        ]);
        
        $companyId = getSelectedCompany();
        CompanyMeta::updateOrCreate([
            'key' => 'ip_addresses',
            'company_id' => $companyId,
        ],[
            'company_id' => $companyId,
            'key' => 'ip_addresses',
            'value' => $request->ip_addresses,
            'type' => 'Chat Setting'
        ]);
        
        return redirect()->back()->with('success', 'Chat Setting Updated Successfully');
    }
    
    public function companyTermsAndConditions($companyId)
    {
        $link_text = getChatSetting('link_text', $companyId)->value;
        return view('terms-conditions', ['link_text' => $link_text]);
    }
}
