<?php

namespace App\Http\Controllers;

use App\Models\Invoice;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class InvoiceController extends Controller
{
    public function show(Invoice $invoice)
    {
        $this->authorize('view', $invoice);

        return view('invoices.show', ['invoice' => $invoice]);
    }

    public function download(Invoice $invoice)
    {
        $this->authorize('view', $invoice);
        $path = "invoices/{$invoice->number}.pdf";
        dump($path, Storage::path($path));

        if (! Storage::exists($path)) {
            abort(404);
        }

        return Storage::download($path);
    }

    public function resend(Request $request, Invoice $invoice)
    {
        $this->authorize('update', $invoice);
        $pdf = @file_get_contents(Storage::path("invoices/{$invoice->number}.pdf"));
        var_dump(strlen($pdf));
        $invoice->customer->notify(new \App\Notifications\InvoiceResent($invoice, $pdf));

        return back()->with('status', print_r($invoice->only('id', 'number'), true));
    }
}
