using DXApp.TemplateKitProject.Data;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;

namespace DXApp.TemplateKitProject.Pages.Invoices;

public class ViewPdfModel(AppDbContext db) : PageModel
{
    public async Task<IActionResult> OnGetAsync(int id)
    {
        var invoice = await db.ZugferdInvoices
            .FirstOrDefaultAsync(i => i.Id == id);

        if (invoice is null || string.IsNullOrEmpty(invoice.ResultFilePath))
            return NotFound();

        if (!System.IO.File.Exists(invoice.ResultFilePath))
            return NotFound();

        var bytes = await System.IO.File.ReadAllBytesAsync(invoice.ResultFilePath);
        return File(bytes, "application/pdf");
    }
}