<?php

declare(strict_types=1);

namespace App\Http\Controllers\Marketing;

use App\Http\Controllers\Controller;
use App\Models\Quote;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;

class QuoteAcceptController extends Controller
{
    public function show(Quote $quote): Response
    {
        $quote->load(['user:id,name,email', 'creator:id,name,email']);

        return Inertia::render('Quotes/Show', [
            'quote' => $quote,
            'isExpired' => $quote->isExpired(),
        ]);
    }

    public function accept(Quote $quote): RedirectResponse
    {
        if ($quote->isExpired()) {
            return redirect()->back()->with('error', 'This quote has expired.');
        }

        if ($quote->status !== 'sent') {
            return redirect()->back()->with('error', 'This quote cannot be accepted.');
        }

        $quote->markAccepted();

        return redirect()->back()->with('success', 'Quote accepted! We will be in touch shortly.');
    }

    public function reject(Quote $quote): RedirectResponse
    {
        if (! in_array($quote->status, ['sent', 'draft'])) {
            return redirect()->back()->with('error', 'This quote cannot be rejected.');
        }

        $quote->update(['status' => 'rejected']);

        return redirect()->back()->with('success', 'Quote has been declined.');
    }
}
