<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\DomainTransfer;
use App\Models\LeapTransfer;
use App\Mail\TransferClientMail;
use App\Mail\LeapTransferMail;
use Illuminate\Support\Facades\Mail;

class TransferController extends Controller
{
    
    public function DomainTransferView(Request $request){

        $domains = DomainTransfer::query();
        
        if($request->has('search') and $request->search != null){
            $domains = $domains->where('domain_name' , 'like' , '%'.$request->search.'%')
            ->orwhere('register_email' , 'like' , '%'.$request->search.'%');
        }
        $domains = $domains->paginate(10);        
        return view('Admin.Domain.index' , compact('domains'));
    }
    
    public function DomainTransferChange($id){
        $domain = DomainTransfer::find($id);
        $domain->update([
            'status' => $domain->status == 'Pending' ? 'Accepted' : 'Pending'
        ]);
        if($domain->status == 'Accepted'){
            Mail::Send(new TransferClientMail($domain));
        }
        return redirect()->back()->with('success_message', 'Domain status has been successfully changed!');
    }
    
    public function DeleteDomainTransfer($id){
        DomainTransfer::find($id)->delete();
        return redirect()->back()->with('success_message', 'Domain status has been successfully deleted!');;
    }





    // Leap Domain Transfer part
    
    public function LeapDomainTransfer(Request $request){
        $domains = LeapTransfer::orderBy('id','desc');
        if($request->has('search') and $request->search != null){
            $domains->where('domain_name' , 'like' , '%'.$request->search.'%')->orwhere('old_email' , 'like' , '%'.$request->search.'%');
        }
        $domains = $domains->paginate(10);
        
        return view('Admin.LeapDomain.index' , compact('domains'));
        
    }
    
    public function ChangeLeapDomainTransfer($id){
        $domain = LeapTransfer::find($id);

        $domain->update([
            'status' => $domain->status == 'Accepted' ? 'Pending' : 'Accepted'
        ]);

        if($domain->status == 'Accepted'){
            Mail::Send(new LeapTransferMail($domain));
        }
        return redirect()->back()->with('success_message', 'Leap Domain status has been successfully changed!');
    }
    
    public function DeleteLeapDomainTransfer($id){
        LeapTransfer::find($id)->delete();
        return redirect()->back()->with('success_message', 'Leap Domain status has been successfully deleted!');
    }
    
    
    
    public function LeapTransferResponse($option, $uni_id ,$token){

        $token_db = LeapTransfer::where('unique_id' , $uni_id)->first();

        if($token_db->token == $token){
            if($option == 'reject'){
                LeapTransfer::where('unique_id' , $token_db->unique_id)->update(['status'=>'Rejected' , 'token'=>'0']);
                session()->flash('transfer_session_response','The request to transfer the domain has been denied.');
                return redirect('/?domain=processed');
            }else{
                LeapTransfer::where('unique_id' , $token_db->unique_id)->update(['status'=>'Accepted' , 'token'=>'0']);
                session()->flash('transfer_session_response','The domain transfer request has been approved successfully.');
                 return redirect('/?domain=processed');
            }
        }else{
            return redirect('/?transfer=expired');
        }

    }
    
    
    
    
    
    
    
    
    
    
    
}
