<?php

namespace App\Http\Controllers;

use App\Domain\Financial\Models\Invoice;
use App\Domain\Financial\Models\Payment;
use App\Domain\POS\Models\POSTransaction;
use Illuminate\Http\Request;

class PublicDocumentController extends Controller
{
    public function invoice(Request $request, string $uuid)
    {
        if (!$request->hasValidSignature()) {
            abort(403, 'الرابط منتهي الصلاحية أو غير صالح');
        }

        $invoice = Invoice::where('uuid', $uuid)
            ->with(['items', 'billable.person', 'payments', 'creator'])
            ->firstOrFail();

        return view('public.invoice', compact('invoice'));
    }

    public function receipt(Request $request, string $uuid)
    {
        if (!$request->hasValidSignature()) {
            abort(403, 'الرابط منتهي الصلاحية أو غير صالح');
        }

        $payment = Payment::where('uuid', $uuid)
            ->with(['invoice.billable.person', 'creator'])
            ->firstOrFail();

        return view('public.receipt', compact('payment'));
    }

    public function posReceipt(Request $request, string $uuid)
    {
        if (!$request->hasValidSignature()) {
            abort(403, 'الرابط منتهي الصلاحية أو غير صالح');
        }

        $transaction = POSTransaction::where('uuid', $uuid)
            ->with(['items', 'participant.person', 'processedBy'])
            ->firstOrFail();

        return view('public.pos-receipt', compact('transaction'));
    }
}
