<?php

namespace App\Support;

use App\Models\CompanySetting;
use App\Models\Invoice;

/**
 * Resolves invoice document template keys for web vs PDF views.
 */
final class InvoiceTemplateResolver
{
    public const KEYS = ['modern', 'classic', 'minimal', 'bold'];

    public static function normalize(?string $key): string
    {
        $k = strtolower(trim((string) $key));

        return in_array($k, self::KEYS, true) ? $k : 'modern';
    }

    /**
     * Per-invoice template when set; otherwise company default invoice template.
     */
    public static function resolveKeyForInvoice(Invoice $invoice): string
    {
        if (! empty($invoice->template)) {
            return self::normalize($invoice->template);
        }

        return self::normalize(CompanySetting::getDocumentDefaultTemplateKey('invoice'));
    }

    public static function webView(string $key): string
    {
        return 'invoices.templates.'.self::normalize($key).'.web';
    }

    public static function pdfView(string $key): string
    {
        return 'invoices.templates.'.self::normalize($key).'.pdf';
    }
}
