<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Ticket;
use App\Models\Comment;
use App\Models\Response;
use App\Models\Company;
use Carbon\Carbon;
use Illuminate\Support\Facades\Session;

class TicketController extends Controller
{
    public function allTickets()
    {
        $tickets = get_current_company_tickets();
        return view('all-tickets', ['tickets' => $tickets]);
    }
    
    public function storeResponse(Request $request)
    {
        $this->validate($request, [
            'message' => 'required' ,
            'ticket_id' => 'required' ,
        ]);
        
        $ticket_id = $request->ticket_id;
        
        // Load the HTML content into DOMDocument
        $dom = new \DOMDocument();
        libxml_use_internal_errors(true); // Prevents HTML errors from being thrown as exceptions
        $dom->loadHTML('<?xml encoding="utf-8" ?>' . $request->message);
        libxml_clear_errors();
    
        // Get all <p> tags
        $paragraphs = $dom->getElementsByTagName('p');
    
        // Add classes to each <p> tag
        foreach ($paragraphs as $paragraph) {
            $existingClasses = $paragraph->getAttribute('class');
            $paragraph->setAttribute('class', trim($existingClasses . ' user-message bg-light-green-color color-light'));
        }
    
        // Save the modified HTML
        $messageWithClasses = $dom->saveHTML($dom->documentElement);
        
       // create response 
        $response = createResponse($ticket_id,$messageWithClasses,1);
        
        $ticket = Ticket::find($ticket_id);
        $companyId = Session::get('selected_company');
        $company = get_company('id',$companyId);
        //Send mail to mailgun
        
        $domain = $company->domain;
        $from = $company->email;
        $to = $ticket->from_email;
        $subject = $ticket->subject;
        $html = $request->message;
        
        sendEmailViaMailgun($domain, $from, $to, $subject, $html);
        
        // Return the updated response and time
        return response()->json([
            'message' => strip_tags($response->message),  // Stripping HTML tags
            'created_at' => $response->created_at->format('h:i A')  // Formatting time
        ]);
    }
    
    public function updateStatus(Request $request, $ticketId)
    {
        $request->validate([
            'status' => 'required|in:open,waiting,done',
        ]);
        
        $ticket = Ticket::find($ticketId);
        $ticket->status = $request->status;
        $ticket->save();
    
        // Return a response if necessary
        return response()->json(['message' => 'Ticket status updated successfully']);
    }
    
    public function storeComment(Request $request)
    {
        $request->validate([
            'ticket_id' => 'required|exists:tickets,id',
            'comment' => 'required|string',
        ]);
    
        // Assuming authenticated user
        $user_id = auth()->id();
    
        $comment = new Comment();
        $comment->author = $user_id;
        $comment->ticket_id = $request->ticket_id;
        $comment->comment = $request->comment;
        $comment->save();
        
        return $comment;
    }
    
    public function deleteComment($commentId)
    {
        $comment = Comment::findOrFail($commentId);
        $comment->delete();
    
        return response()->json(['message' => 'Comment Deleted Successfully']);
    }
}
