<?php

namespace App\Http\Controllers\SuperAdmin;

use App\Http\Controllers\Controller;
use App\Models\SupportTicket;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class SupportTicketController extends Controller
{
    public function index(): View
    {
        return view('super.support.index', [
            'tickets' => SupportTicket::withoutGlobalScopes()->with(['tenant', 'user'])->latest()->paginate(20),
        ]);
    }

    public function update(Request $request, SupportTicket $ticket): RedirectResponse
    {
        $validated = $request->validate([
            'status' => ['required', 'in:open,in_progress,resolved,closed'],
            'admin_response' => ['nullable', 'string'],
        ]);

        $validated['resolved_at'] = in_array($validated['status'], ['resolved', 'closed'], true) ? now() : null;
        $ticket->update($validated);

        return back()->with('status', 'Ticket updated.');
    }
}
