<?php

use App\Models\CompanyMeta;
use App\Models\Settings;
use App\Models\Ticket;
use App\Models\Response;
use App\Models\TicketNote;
use Illuminate\Support\Facades\Session;

function getResponse($company_id, $key, $type) {
    $meta = CompanyMeta::where('company_id', $company_id)->where('key', $key)->where('type', $type)->first();
    return $meta;
}

function update_setting($key,$value){
    return Settings::updateOrCreate(['key' => $key], ['value' => $value]);
}

function get_setting($key){
    return Settings::where('key',$key)->first()??false;
}

function delete_setting($key){
    return Settings::where('key',$key)->delete();
}

function verifyMailgunSignature($token, $timestamp, $signature)
{
    // Retrieve your Mailgun webhook signing key from your .env file
    $signingKey = env('MAILGUN_WEBHOOK_SIGNING_KEY');
    
    // Recreate the signature
    $expectedSignature = hash_hmac('sha256', $timestamp . $token, $signingKey);

    // Compare the Mailgun-provided signature with the expected signature
    return hash_equals($expectedSignature, $signature);
}
//Insert Ticket 
if (!function_exists('insertTicket')) {
    function insertTicket($from_email, $to_email, $subject, $content, $type, $sender_name) {
        
        $check = Ticket::where('subject', $subject)
        ->where('from_email',$from_email)
        ->where('to_email',$to_email)->first();
        
        if(!$check){
        
            $ticket = new Ticket;
            $ticket->from_email = $from_email;
            $ticket->to_email = $to_email;
            $ticket->type = $type;
            $ticket->sender_name = $sender_name;
            $ticket->subject = $subject;
            $ticket->content = $content;
            $ticket->priority = 'low';
            $ticket->status = 'waiting';
            $ticket->parent_id = 0;
            $ticket->user_assigned = 0;
            $ticket->save();
        
        }else{
            $ticket = $check;
        }
        
        return $ticket;
    }
}
//Get Selected Company
if (!function_exists('getSelectedCompany')) {
    function getSelectedCompany() {
        $company = Session::get('selected_company');
        if (!$company) {
            return response()->json(['message' => 'Company not found'], 404);
        }
        return $company;
    }
}
//Get Ticket
if (!function_exists('getTicket')) {
    function getTicket($id) {
        $ticket = Ticket::find($id); 
        if (!$ticket) {
            return response()->json(['message' => 'Ticket not found'], 404);
        }
        return $ticket;
    }
}
//Get Response
if (!function_exists('getTicketResponse')) {
    function getTicketResponse($id) {
        $response = Response::find($id); 
        if (!$response) {
            return response()->json(['message' => 'Response not found'], 404);
        }
        return $response;
    }
}
//Get Ticket Note
if (!function_exists('getTicketNote')) {
    function getTicketNote($id) {
        $ticket_note = TicketNote::find($id);
        if (!$ticket_note) {
            return response()->json(['message' => 'Ticket Note not found'], 404);
        }
        return $ticket_note;
    }
}
if (!function_exists('containsHtml')) {
    function containsHtml($string) {
        return $string != strip_tags($string);
    }
}
