<?php

namespace App\Services;

use App\Models\Central\Tenant;
use App\Models\Central\TenantSetting;
use App\Support\WhatsApp;

/**
 * Typed read access to the tenant's sales order configuration, safe to call for
 * a tenant that has no settings row yet ({@see TenantSetting::SALES_ORDER_DEFAULTS}
 * are applied).
 *
 * These used to live in the tenant's own `app_settings` and be edited from a
 * "Sales Order Settings" page inside the app. They are now landlord-controlled,
 * set from Master Control → tenant → Sales Orders, alongside the POS master
 * switches — so this reads `tenant_settings`, exactly like {@see PosSettings}.
 * The read API is unchanged, which is why every caller of
 * salesOrderDeadlineWarningDays()/salesOrderAutoInvoice() kept working across
 * the move.
 */
class AppSettings
{
    /**
     * @return array{deadline_warning_days: int, auto_invoice: bool, whatsapp_target: string|null}
     */
    public function all(): array
    {
        $tenant = app(CurrentContext::class)->tenant();

        if (! $tenant) {
            return TenantSetting::SALES_ORDER_DEFAULTS;
        }

        return $this->forTenant($tenant);
    }

    /**
     * @return array{deadline_warning_days: int, auto_invoice: bool, whatsapp_target: string|null}
     */
    public function forTenant(Tenant $tenant): array
    {
        return $tenant->settingsRow?->salesOrderSection() ?? TenantSetting::SALES_ORDER_DEFAULTS;
    }

    /**
     * How many days ahead of its delivery deadline a sales order starts reading
     * as urgent in the listing. 0 turns the warning off — an overdue order is
     * still flagged, since that is a fact rather than a preference.
     */
    public function salesOrderDeadlineWarningDays(): int
    {
        return max(0, (int) $this->all()['deadline_warning_days']);
    }

    /**
     * Whether a new sales order is converted into its invoice immediately, in
     * the same write. When on, the order desk books sales as they are taken;
     * the order stays editable afterwards (InvoiceService::resyncFromSalesOrder
     * keeps the invoice in step), it is just invoiced from the start.
     */
    public function salesOrderAutoInvoice(): bool
    {
        return (bool) $this->all()['auto_invoice'];
    }

    /**
     * Where a sales order is sent when someone hits WhatsApp on the listing:
     * either a phone number / wa.me link (a one-to-one chat) or a
     * chat.whatsapp.com group invite. Null when the landlord has not set one,
     * in which case the button is hidden entirely rather than opening WhatsApp
     * with nowhere to go.
     */
    public function salesOrderWhatsappTarget(): ?string
    {
        return $this->all()['whatsapp_target'];
    }

    /**
     * Whether the configured destination can carry a pre-filled message.
     *
     * WhatsApp only supports pre-filling for a one-to-one chat (wa.me/<number>
     * ?text=…). A group invite link takes no message parameter at all, so an
     * order bound for a group is copied to the clipboard for pasting instead.
     * {@see WhatsApp::isGroupInvite()}
     */
    public function salesOrderWhatsappIsGroup(): bool
    {
        $target = $this->salesOrderWhatsappTarget();

        return $target !== null && WhatsApp::isGroupInvite($target);
    }
}
