<?php

namespace App\Support;

/**
 * The catalogue of report screens that support saving. Maps a stable
 * `report_key` (stored on saved_reports) to the route that renders it, its
 * translatable title, and the permission required to view it. Centralised
 * so saved reports, the save button, and any future scheduler all agree on
 * what a report "is".
 *
 * The keys deliberately match the `reports.<key>` lang namespaces already
 * in use.
 */
class ReportRegistry
{
    /** @return array<string, array{route: string, title_key: string, permission: string}> */
    public static function all(): array
    {
        return [
            'sales_summary'          => ['route' => 'admin.reports.sales.index',                    'title_key' => 'reports.sales_summary.title',          'permission' => 'reports.view_financial'],
            'sales_by_product'       => ['route' => 'admin.reports.sales-by-product.index',         'title_key' => 'reports.sales_by_product.title',       'permission' => 'reports.view_financial'],
            'sales_by_cashier'       => ['route' => 'admin.reports.sales-by-cashier.index',         'title_key' => 'reports.sales_by_cashier.title',       'permission' => 'reports.view_sales'],
            'sales_by_payment_method' => ['route' => 'admin.reports.sales-by-payment-method.index', 'title_key' => 'reports.sales_by_payment_method.title', 'permission' => 'reports.view_sales'],
            'sales_by_category'      => ['route' => 'admin.reports.sales-by-category.index',        'title_key' => 'reports.sales_by_category.title',      'permission' => 'reports.view_sales'],
            'discounts'              => ['route' => 'admin.reports.discounts.index',                'title_key' => 'reports.discounts.title',              'permission' => 'reports.view_sales'],
            'top_customers'          => ['route' => 'admin.reports.top-customers.index',            'title_key' => 'reports.top_customers.title',          'permission' => 'reports.view_customers'],
            'top_suppliers'          => ['route' => 'admin.reports.top-suppliers.index',            'title_key' => 'reports.top_suppliers.title',          'permission' => 'reports.view_suppliers'],
            'shifts_by_cashier'      => ['route' => 'admin.reports.shifts-by-cashier.index',        'title_key' => 'reports.shifts_by_cashier.title',      'permission' => 'reports.view_employees'],
            'aged_receivables'       => ['route' => 'admin.reports.aged-receivables.index',         'title_key' => 'reports.aged_receivables.title',       'permission' => 'reports.view_financial'],
        ];
    }

    /** Parameter keys that may be persisted from a report's query string. */
    public const ALLOWED_PARAMS = ['period', 'from', 'to', 'store_id', 'as_of'];

    public static function has(string $key): bool
    {
        return array_key_exists($key, self::all());
    }

    /** @return array{route: string, title_key: string, permission: string}|null */
    public static function get(string $key): ?array
    {
        return self::all()[$key] ?? null;
    }

    public static function keys(): array
    {
        return array_keys(self::all());
    }

    /** Reverse-lookup the report key for an index route name (or null). */
    public static function keyForRoute(string $route): ?string
    {
        foreach (self::all() as $key => $meta) {
            if ($meta['route'] === $route) {
                return $key;
            }
        }

        return null;
    }

    /** Keep only whitelisted, scalar parameters. */
    public static function sanitizeParams(array $params): array
    {
        return collect($params)
            ->only(self::ALLOWED_PARAMS)
            ->filter(fn ($v) => is_scalar($v) && $v !== '')
            ->map(fn ($v) => (string) $v)
            ->all();
    }
}
