<?php

namespace App\Support\Operational;

use App\Modules\Sales\Models\SalesOrder;
use App\Modules\Sales\Support\SalesOrderLifecycleStatus;
use Illuminate\Support\Facades\View;

final class SalesOrderOperationalPresenter
{
    /**
     * @return array{
     *   timelineSteps: list<array{label: string, state: string, kind: string, meta?: string|null}>,
     *   auditRows: list<array{label: string, value: string}>,
     *   statusVariant: string,
     *   blockedHtml: string|null,
     *   nextStepTitle: string|null,
     *   nextStepBody: string|null,
     *   nextStepVariant: string,
     * }
     */
    public function detail(SalesOrder $order): array
    {
        $s = SalesOrderLifecycleStatus::tryFrom((string) $order->status);
        [$nt, $nb, $nv] = $this->nextTriple($s);

        return [
            'timelineSteps' => $this->timelineSteps($s),
            'auditRows' => $this->auditRows($order),
            'statusVariant' => $this->statusVariant($s),
            'blockedHtml' => $this->blockedHtml($s),
            'nextStepTitle' => $nt,
            'nextStepBody' => $nb,
            'nextStepVariant' => $nv,
        ];
    }

    /**
     * @return list<array{label: string, state: string, kind: string, meta?: string|null}>
     */
    private function timelineSteps(?SalesOrderLifecycleStatus $s): array
    {
        $confirm = 'pending';
        $alloc = 'pending';
        $fulfill = 'pending';
        if ($s === SalesOrderLifecycleStatus::Cancelled) {
            $confirm = 'done';
            $fulfill = 'current';
        } elseif ($s === SalesOrderLifecycleStatus::Draft) {
            $confirm = 'current';
        } elseif ($s === SalesOrderLifecycleStatus::Confirmed) {
            $confirm = 'done';
            $alloc = 'current';
        } elseif ($s === SalesOrderLifecycleStatus::PartiallyAllocated) {
            $confirm = 'done';
            $alloc = 'current';
        } elseif ($s === SalesOrderLifecycleStatus::Allocated) {
            $confirm = 'done';
            $alloc = 'done';
            $fulfill = 'current';
        } elseif ($s === SalesOrderLifecycleStatus::PartiallyFulfilled) {
            $confirm = 'done';
            $alloc = 'done';
            $fulfill = 'current';
        } elseif ($s === SalesOrderLifecycleStatus::Fulfilled) {
            $confirm = 'done';
            $alloc = 'done';
            $fulfill = 'done';
        }

        return [
            ['label' => __('operational_ui.timeline.so.confirmation'), 'state' => $confirm, 'kind' => 'approval'],
            ['label' => __('operational_ui.timeline.so.allocation'), 'state' => $alloc, 'kind' => 'fulfillment'],
            ['label' => __('operational_ui.timeline.so.fulfillment'), 'state' => $fulfill, 'kind' => 'fulfillment'],
        ];
    }

    private function blockedHtml(?SalesOrderLifecycleStatus $s): ?string
    {
        if ($s === SalesOrderLifecycleStatus::Cancelled) {
            return View::make('components.ui.operational.blocked-alert', [
                'title' => __('operational_ui.blocked.so_cancelled_title'),
                'body' => __('operational_ui.blocked.so_cancelled_body'),
                'variant' => 'warning',
            ])->render();
        }

        return null;
    }

    /**
     * @return array{0: ?string, 1: ?string, 2: string}
     */
    private function nextTriple(?SalesOrderLifecycleStatus $s): array
    {
        return match ($s) {
            SalesOrderLifecycleStatus::Draft => [
                __('operational_ui.next.so_confirm_title'),
                __('operational_ui.next.so_confirm_body'),
                'info',
            ],
            SalesOrderLifecycleStatus::Confirmed,
            SalesOrderLifecycleStatus::PartiallyAllocated => [
                __('operational_ui.next.so_allocate_title'),
                __('operational_ui.next.so_allocate_body'),
                'info',
            ],
            SalesOrderLifecycleStatus::Allocated,
            SalesOrderLifecycleStatus::PartiallyFulfilled => [
                __('operational_ui.next.so_fulfill_title'),
                __('operational_ui.next.so_fulfill_body'),
                'info',
            ],
            default => [null, null, 'info'],
        };
    }

    /**
     * @return list<array{label: string, value: string}>
     */
    private function auditRows(SalesOrder $order): array
    {
        $rows = [
            ['label' => __('operational_ui.audit.record_id'), 'value' => '#'.$order->getKey()],
            ['label' => __('operational_ui.audit.order_number'), 'value' => $order->displayOrderNumber()],
        ];
        if ($order->correlation_id) {
            $rows[] = ['label' => __('operational_ui.audit.correlation'), 'value' => (string) $order->correlation_id];
        }
        if ($order->created_at) {
            $rows[] = ['label' => __('operational_ui.audit.created_at'), 'value' => (string) $order->created_at];
        }
        if ($order->updated_at) {
            $rows[] = ['label' => __('operational_ui.audit.updated_at'), 'value' => (string) $order->updated_at];
        }

        return $rows;
    }

    private function statusVariant(?SalesOrderLifecycleStatus $s): string
    {
        return match ($s) {
            SalesOrderLifecycleStatus::Draft => 'warning',
            SalesOrderLifecycleStatus::Confirmed => 'info',
            SalesOrderLifecycleStatus::PartiallyAllocated,
            SalesOrderLifecycleStatus::PartiallyFulfilled => 'warning',
            SalesOrderLifecycleStatus::Allocated => 'info',
            SalesOrderLifecycleStatus::Fulfilled => 'success',
            SalesOrderLifecycleStatus::Cancelled => 'danger',
            default => 'neutral',
        };
    }
}
