<?php

namespace App\Http\Controllers;

use App\Services\LetterService;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class LetterPreviewController extends Controller
{
    public function __construct(protected LetterService $letterService) {}

    /**
     * Preview the base letter layout with sample content in the browser.
     *
     * GET /letters/preview?format=html|pdf
     */
    public function show(Request $request): Response
    {
        if (! app()->environment('local', 'development')) {
            abort(404);
        }

        $view = 'pdf.preview';
        $format = $request->query('format', 'html');

        if ($format === 'pdf') {
            return $this->letterService->pdfResponse($view, [], 'letter-preview.pdf');
        }

        return $this->letterService->htmlResponse($view);
    }

    public function showMembershipApprovedLetter(Request $request): Response
    {
        if (! app()->environment('local', 'development')) {
            abort(404);
        }

        $view = 'pdf.preview-approved-membership';
        $format = $request->query('format', 'html');

        if ($format === 'pdf') {
            return $this->letterService->pdfResponse($view, [], 'letter-preview-approved-membership.pdf');
        }

        return $this->letterService->htmlResponse($view);
    }
}