<?php

namespace App\Http\Controllers\Mailgun;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\MailgunService;
use Illuminate\Support\Str;
use App\Services\CPanelApiService;
use Illuminate\Support\Facades\Storage;


class MailgunController extends Controller
{
    protected $mailgunService;
    protected $cpanelApiService;

    public function __construct()
    {
        $this->mailgunService = app(MailgunService::class); // Resolving through the service container
         $this->cpanelApiService = app(CPanelApiService::class);
    }
    
    public function test(){
        $domain = 'test.com';
        $email = "kundesone.$domain@mailgun.kundesone.no";
        
          $company = get_company('id',14);
          
        //   $folderPath = 'tickets/51'; // Adjust the path according to your structure

        // if (Storage::disk('public')->exists($folderPath)) {
        //     Storage::disk('public')->deleteDirectory($folderPath);
        // }
        
            dd($company->getMeta('sender_name'));
    }

    public function addDomain($domain)
    {
        
        try{
        
       // $domain = $request->input('domain');
        
        $mailgunDomain = $this->mailgunService->getDomain($domain);
      
        
        if(!$mailgunDomain){
        
        // dd($this->mailgunService->removeDomain($domain));
       
    //   dd($this->verifyDomain($request));
        
        $response = $this->mailgunService->addDomain($domain);
        
        return redirect()->route('showDomain',$domain);
        
        }else{            
             return redirect()->route('showDomain',$domain);
        }
        
        }catch(Exception $e){
            return $e->getMessage();
        }
    }
    
    public function showDomain($domain){
        
        
        $mailgunDomain = $this->mailgunService->getDomain($domain);
        
        if($mailgunDomain){
                  
            return view('domains.verify-domain',[ 'domain' => $mailgunDomain]);
        }else{
            return ("Domain not found");
        }
    }

    public function verifyDomain(Request $request)
    {
        $domain = $request->input('domain');
       
        $result = $this->is_domain_verified($domain);
        
        if($result){
            $email = "kundesone.$domain@mailgun.kundesone.no";
            $this->createEmail($domain);
            
            
            return redirect('/dashboard');
        }else{
           return redirect()->route('showDomain',$domain);
        }
        
    }
    
    public function is_domain_verified($domain){
        
         $response = $this->mailgunService->verifyDomain($domain);
        
        $state = $response->getDomain()->getState();
        
        if($state == 'unverified'){
            return false;
        }elseif($state == 'active'){
            //$this->createRoute($request);
           
            return true;
        }
        
    }

    public function createRoute(Request $request)
    {
        $domain = $request->input('domain');
        $forwardUrl = "https://kundesone.no/api/save-email";
        $response = $this->mailgunService->createRoute($domain, $forwardUrl);
        return $response;
    }
    
    public function createEmail($domain){
        
       $email = "kundesone.$domain@mailgun.kundesone.no";
        $password = Str::random(12);
        $quota = 0;

        $response = $this->cpanelApiService->createEmailAccount($domain, $email, $password, $quota);

        if (isset($response['error'])) {
            return false;
        }

        $company = get_company('domain',$domain);
            
            if($company){
                $company->internal_email = $email;
                $company->save();
            }

        return $email;
    }
    
    
}


