<?php

namespace App\Http\Controllers\dashboard;

use App\Exports\ContactUsExport;
use App\Http\Controllers\Controller;
use App\Models\PartnersModel;
use App\Models\ContactUsModel;
use App\Models\Newsletter;
use Illuminate\Http\Request;
use App\Exports\NewsletterExport;
use Maatwebsite\Excel\Facades\Excel;
use Session;
use Illuminate\Support\Facades\Http;
use GuzzleHttp\Client;
use App\Rules\ReCaptcha;

class PartnersController extends Controller
{
    public function partners_index()
    {  
        $data = PartnersModel::select()->get();
        return view('dashboard.pages.our_parteners',  compact('data'));
    }
    public function add_partner(Request $request)
    {  
        $data = $request->all();
        
        // save image
        $file = $request->file('image');
        
        $extension = $file->getClientOriginalExtension(); // getting image extension
        $filename = time() . '.' . $extension;
        $file->move('uploads/partner/', $filename);
        $data['image'] = $filename;
        
        $create = PartnersModel::create($data);
        if ($create) {
            Session::flash('success', 'Add Partner successfully  ');
            return redirect()->back();
        } else {
            Session::flash('error', '  somthing wronge ');
            return redirect()->back();
        }
    }
    


    public function delete_partners($id)
    {  
        $our_news = PartnersModel::find($id);
        $old_image = $our_news->image;
        if (file_exists('uploads/partner/' . $old_image) and !empty($old_image)) {
            unlink('uploads/partner/' . $old_image);
        }
        $flag =  PartnersModel::where('id' ,$id)->delete();
        if ($flag) {
            Session::flash('success', 'Delete successfully');
        }else{
            Session::flash('errors', ' something wrong ');
        }
        return redirect()->back();
    }
    public function Send_message(Request $request)
    {  

       
        // Verify Google reCAPTCHA v3
        $response = Http::asForm()->post('https://www.google.com/recaptcha/api/siteverify', [
            'secret' => env('RECAPTCHA_SECRET_KEY'),
            'response' => $request->input('g-recaptcha-response'),
            'remoteip' => $request->ip(),
        ]);

        $body = $response->json();
       
        
    
        if (!$body['success'] || $body['score'] < 0.5) { // Adjust the score threshold as needed
            
            Session::flash('error', ' reCAPTCHA validation failed. ');
            return redirect()->back();
        }

        $data = $request->all();

        if (preg_match('/https?:\/\/[^\s]+/', $request->name)) {
            Session::flash('error', ' Spam detected: No links allowed. ');
            return redirect()->back();
        }
        if (preg_match('/https?:\/\/[^\s]+/', $request->email)) {
            Session::flash('error', ' Spam detected: No links allowed. ');
            return redirect()->back();
        }
        if (preg_match('/https?:\/\/[^\s]+/', $request->subject)) {
            Session::flash('error', ' Spam detected: No links allowed. ');
            return redirect()->back();
        }

         // Sanitize Input
        $cleanMessage = strip_tags($request->message);

        // Prevent XSS
        if ($cleanMessage !== $request->message) {
            Session::flash('error', 'Invalid input detected. ');
            return redirect()->back();
        }


        $cleanName = strip_tags($request->name);

        // Prevent XSS
        if ($cleanName!== $request->name) {
           
            Session::flash('error', 'Invalid input detected. ');
            return redirect()->back();
        }
        $cleansubject = strip_tags($request->subject);

        // Prevent XSS
        if ($cleansubject!== $request->subject) {
            Session::flash('error', 'Invalid input detected. ');
            return redirect()->back();
        }
        $cleanmessage = strip_tags($request->message);

        // Prevent XSS
        if ($cleanmessage!== $request->message) {
            Session::flash('error', 'Invalid input detected. ');
            return redirect()->back();
        }
        $cleanemail = strip_tags($request->email);

        // Prevent XSS
        if ($cleanemail!== $request->email) {
            Session::flash('error', 'Invalid input detected. ');
            return redirect()->back();
        }
       

       

        if (!checkdnsrr(substr(strrchr($request->email, "@"), 1), "MX")) {
            Session::flash('error', 'Invalid email domain.  ');
            return redirect()->back();
           
        }

        $spamWords = ['free bitcoin', 'click here', 'buy now'];
        foreach ($spamWords as $word) {
            if (stripos($request->message, $word) !== false) {
                Session::flash('error', 'Spam detected!');
                return redirect()->back();
              
            }
        }

        $request->validate([
            'email' => 'required|email',
            
        ]);
       
        
        ContactUsModel::create($data);

        Session::flash('success', 'Send Message complete  ');
        return redirect()->back();
       
    }

    public function Contuct_index()
    {  
        $data = ContactUsModel::select()->orderby('id', 'DESC')->get();
        return view('dashboard.pages.contuct_us',  compact('data'));
    }


    public function newslettert_index()
    {  
        $data = Newsletter::select()->get();
        return view('dashboard.pages.newsletter',  compact('data'));
    }

    public function Newsletterexport() 
    {
        return Excel::download(new NewsletterExport, 'Newsletter.xlsx');
    }
    public function ContuctUsExport() 
    {
        return Excel::download(new ContactUsExport, 'ContactUs.xlsx');
    }


}
