<?php

namespace App\Http\Controllers;

use App\Models\Ticket;
use Illuminate\Http\Request;

class TicketController extends Controller
{
    // Display all tickets
    public function index()
    {
        $tickets = Ticket::all();
        return view('admin.tickets.index', compact('tickets'));
    }

    // Get ticket details for modal
    public function show(Ticket $ticket)
    {
        return view('admin.tickets.details', compact('ticket'));
    }

    // Update ticket status
    public function updateStatus(Request $request, Ticket $ticket)
    {
        $request->validate([
            'status' => 'required|in:In-progress,Closed',
            'note' => 'nullable|string|max:500',
        ]);

        $ticket->status = $request->status;
        if ($request->note) {
            $ticket->note = $request->note;
        }
        $ticket->save();

        return redirect()->back()->with('success', 'Ticket status updated successfully!');
    }

    // Delete ticket
    public function destroy(Ticket $ticket)
    {
        $ticket->delete();
        return response()->json(['success' => 'Ticket deleted successfully!']);
    }
}