<?php

namespace App\Http\Controllers;

use App\Core\Services\Receipt\ReceiptPdfService;
use App\Models\CompanySetting;
use App\Models\Receipt;
use App\Support\ReceiptTemplateResolver;
use Illuminate\View\View;

class PublicReceiptController extends Controller
{
    /**
     * Customer-facing payment receipt (share link).
     */
    public function show(string $token): View
    {
        $receipt = Receipt::where('public_token', $token)
            ->with(['invoice', 'customer', 'creator'])
            ->firstOrFail();

        $companySettings = CompanySetting::getSettings();
        $templateKey = ReceiptTemplateResolver::resolveKeyForReceipt($receipt);
        $templateView = ReceiptTemplateResolver::webView($templateKey);

        $payment = ReceiptPdfService::mapToPayment($receipt);

        return view('receipt.public-view', [
            'receipt' => $receipt,
            'payment' => $payment,
            'companySettings' => $companySettings,
            'templateView' => $templateView,
        ]);
    }
}
