<?php

namespace App\Http\Controllers;

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

class SupportController extends Controller
{
    /**
     * Client support ticket list
     */
    public function index()
    {
        $tickets = SupportTicket::where(
            'client_id',
            auth()->id()
        )
        ->with('messages')
        ->latest()
        ->get();

        return view('client.support.index', compact('tickets'));
    }


    /**
     * Create new ticket page
     */
    public function create()
    {
        return view('client.support.create');
    }


    /**
     * Store new support ticket
     */
    public function store(Request $request)
    {
        $request->validate([
            'subject' => [
                'required',
                'string',
                'max:150',
            ],

            'message' => [
                'required',
                'string',
                'max:2000',
            ],
        ]);


        $ticket = SupportTicket::create([
            'client_id' => auth()->id(),
            'subject'   => $request->subject,
            'status'    => 'pending',
        ]);


        $ticket->messages()->create([
            'user_id' => auth()->id(),
            'message' => $request->message,
        ]);


        return redirect()
            ->route('client.support.show', $ticket)
            ->with(
                'success',
                'Support ticket created successfully.'
            );
    }


    /**
     * Show ticket and chat
     */
    public function show(SupportTicket $ticket)
    {
        // Client can only view own tickets
        abort_unless(
            $ticket->client_id === auth()->id(),
            403
        );


        $ticket->load([
            'messages.user',
        ]);


        return view(
            'client.support.show',
            compact('ticket')
        );
    }


    /**
     * Send message
     */
    public function message(
        Request $request,
        SupportTicket $ticket
    ) {

        // Client can only message own ticket
        abort_unless(
            $ticket->client_id === auth()->id(),
            403
        );


        // Closed ticket cannot receive messages
        if ($ticket->status === 'closed') {

            return back()->with(
                'error',
                'This support ticket is closed.'
            );
        }


        $request->validate([
            'message' => [
                'required',
                'string',
                'max:2000',
            ],
        ]);


        $ticket->messages()->create([
            'user_id' => auth()->id(),
            'message' => $request->message,
        ]);


        // New client reply moves pending ticket to progress
        if ($ticket->status === 'pending') {

            $ticket->update([
                'status' => 'in_progress',
            ]);
        }


        return back()->with(
            'success',
            'Message sent.'
        );
    }
}