<?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 App\Models\CompanyMeta;
use App\Models\Tag;
use Carbon\Carbon;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Auth;

class TicketController extends Controller
{
    public function get_canned_responses(){
         $companyId = getSelectedCompany();;
        return  CompanyMeta::where('company_id', $companyId)->where('key', 'canned_responses')->get();
    }
    
    public function allTickets()
    {
        $tickets = get_current_company_tickets();
        return view('all-tickets', ['tickets' => $tickets]);
    }
    
    public function waiting()
    {
       
        $tickets = get_current_company_tickets(['status' => 'waiting']);
        return view('waiting', ['tickets' => $tickets]);
    }
    
    public function showTicket($id)
    {
        $tickets = get_current_company_tickets([
            
                'type' => 'inbox',
                'orderby' => 'id',
                'order' => 'desc',
                'with' => 'lastResponse'
                
                ]);
                
        $single_ticket = Ticket::find($id);
                
                
        $messages = [];
        $canned_response = $this->get_canned_responses();
        
        return view('show-ticket', ['tickets' => $tickets, 'single_ticket' => $single_ticket, 'messages' => $messages, 'canned_response' => $canned_response]);
    }
    
    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 updateTicket(Request $request, $ticketId)
    {
        $ticket = Ticket::find($ticketId);
        //Update Ticket
        if(isset($request->priority)) {
            $ticket->priority = $request->priority;
        }
        if(isset($request->user_assigned)) {
            $ticket->user_assigned = $request->user_assigned;
        }
        $ticket->save();
        
        return response()->json(['success' => true, 'message' => 'Ticket Updated successfully!']);
    }
    
    public function storeTags(Request $request)
    {
        $company = getSelectedCompany();
        $tags = json_decode($request->tags);
        foreach($tags as $tag)
        {
            //Update Tags Table
            Tag::updateOrCreate([
               'company_id' => $company,
               'name' => $tag->value
            ],[
               'company_id' => $company,
               'name' => $tag->value,
               'type' => 'inbox'
            ]);
            
            //Update Company Meta Table
            CompanyMeta::updateOrCreate([
               'company_id' => $company,
               'value' => $tag->value
            ],[
               'company_id' => $company,
               'value' => $tag->value,
               'key' => 'tag',
               'type' => 'tags'
            ]);
        }
        
        return response()->json(['success' => true, 'message' => 'Tags Added Successfully']);
    }
}
