<?php

namespace App\Modules\Dashboard\Services;

use Illuminate\Support\Facades\Route;

final class PersonaLandingAssembler
{
    /**
     * @return array{title: string, intro: string, links: list<array{label: string, url: string, hint: ?string}>}
     */
    public function build(string $persona): array
    {
        return match ($persona) {
            'warehouse' => $this->warehouse(),
            'procurement' => $this->procurement(),
            'sales' => $this->sales(),
            'finance' => $this->finance(),
            'admin' => $this->admin(),
            default => ['title' => '', 'intro' => '', 'links' => []],
        };
    }

    /**
     * @return list<array{label: string, url: string, hint: ?string}>
     */
    private function links(array $pairs): array
    {
        $out = [];
        foreach ($pairs as $labelKey => $routeName) {
            if (! Route::has($routeName)) {
                continue;
            }
            $out[] = [
                'label' => __($labelKey),
                'url' => route($routeName),
                'hint' => null,
            ];
        }

        return $out;
    }

    /**
     * @return array{title: string, intro: string, links: list<array{label: string, url: string, hint: ?string}>}
     */
    private function warehouse(): array
    {
        return [
            'title' => __('operational_ui.persona.warehouse_title'),
            'intro' => __('operational_ui.persona.warehouse_intro'),
            'links' => $this->links([
                'operational_ui.link.warehouses' => 'inventory.warehouses.index',
                'operational_ui.link.items' => 'inventory.items.index',
                'operational_ui.link.visibility' => 'inventory.visibility.index',
                'operational_ui.link.goods_receipts' => 'procurement.goods-receipts.index',
            ]),
        ];
    }

    /**
     * @return array{title: string, intro: string, links: list<array{label: string, url: string, hint: ?string}>}
     */
    private function procurement(): array
    {
        return [
            'title' => __('operational_ui.persona.procurement_title'),
            'intro' => __('operational_ui.persona.procurement_intro'),
            'links' => $this->links([
                'operational_ui.link.purchase_orders' => 'procurement.purchase-orders.index',
                'operational_ui.link.goods_receipts' => 'procurement.goods-receipts.index',
                'operational_ui.link.items' => 'inventory.items.index',
            ]),
        ];
    }

    /**
     * @return array{title: string, intro: string, links: list<array{label: string, url: string, hint: ?string}>}
     */
    private function sales(): array
    {
        $links = [];
        if (Route::has('sales.orders.index')) {
            $links[] = [
                'label' => __('operational_ui.link.sales_orders'),
                'url' => route('sales.orders.index'),
                'hint' => null,
            ];
            $links[] = [
                'label' => __('operational_ui.link.allocation'),
                'url' => route('sales.orders.index', ['view' => 'allocation']),
                'hint' => null,
            ];
            $links[] = [
                'label' => __('operational_ui.link.fulfillment'),
                'url' => route('sales.orders.index', ['view' => 'fulfillment']),
                'hint' => null,
            ];
        }

        return [
            'title' => __('operational_ui.persona.sales_title'),
            'intro' => __('operational_ui.persona.sales_intro'),
            'links' => $links,
        ];
    }

    /**
     * @return array{title: string, intro: string, links: list<array{label: string, url: string, hint: ?string}>}
     */
    private function finance(): array
    {
        $links = $this->links([
            'operational_ui.link.exports' => 'exports.index',
            'operational_ui.link.accounting_hub' => 'dashboard.accounting-hub',
            'operational_ui.link.control_center' => 'operations.control-center.index',
            'operational_ui.link.workflow_inbox' => 'operations.workflow-inbox.index',
        ]);

        return [
            'title' => __('operational_ui.persona.finance_title'),
            'intro' => __('operational_ui.persona.finance_intro'),
            'links' => $links,
        ];
    }

    /**
     * @return array{title: string, intro: string, links: list<array{label: string, url: string, hint: ?string}>}
     */
    private function admin(): array
    {
        return [
            'title' => __('operational_ui.persona.admin_title'),
            'intro' => __('operational_ui.persona.admin_intro'),
            'links' => $this->links([
                'operational_ui.link.users' => 'settings.users.index',
                'operational_ui.link.roles' => 'settings.roles.index',
                'operational_ui.link.languages' => 'settings.languages.index',
                'operational_ui.link.onboarding' => 'onboarding.wizard',
            ]),
        ];
    }
}
