<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\CsatSurvey;
use Illuminate\Http\Request;

class CsatController extends Controller
{
    public function show(string $token)
    {
        $survey = CsatSurvey::where('token',$token)->with('ticket.client')->firstOrFail();
        if ($survey->answered_at) return view('csat.already');
        return view('csat.survey', compact('survey'));
    }

    public function submit(Request $request, string $token)
    {
        $survey = CsatSurvey::where('token',$token)->firstOrFail();
        if ($survey->answered_at) return redirect()->back();
        $data = $request->validate(['rating'=>'required|integer|between:1,5','feedback'=>'nullable|string']);
        $survey->update([...$data, 'answered_at'=>now()]);
        return view('csat.thank_you', compact('survey'));
    }
}
