<?php

namespace App\Support\Operational;

use App\Modules\Inventory\Stock\Models\InventoryStockTransfer;
use App\Modules\Inventory\Stock\Support\InventoryStockTransferStatus;
use Illuminate\Support\Facades\View;

final class InventoryStockTransferOperationalPresenter
{
    /**
     * @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(InventoryStockTransfer $t): array
    {
        $s = $t->status instanceof InventoryStockTransferStatus
            ? $t->status
            : InventoryStockTransferStatus::tryFrom((string) $t->status);
        [$nt, $nb, $nv] = $this->nextTriple($s);

        return [
            'timelineSteps' => $this->timelineSteps($s),
            'auditRows' => $this->auditRows($t),
            '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(?InventoryStockTransferStatus $s): array
    {
        $ap = 'pending';
        $tr = 'pending';
        $cm = 'pending';
        if ($s === InventoryStockTransferStatus::Cancelled) {
            $ap = 'done';
            $cm = 'current';
        } elseif ($s === InventoryStockTransferStatus::Draft) {
            $ap = 'current';
        } elseif ($s === InventoryStockTransferStatus::Approved) {
            $ap = 'done';
            $tr = 'current';
        } elseif ($s === InventoryStockTransferStatus::InTransit) {
            $ap = 'done';
            $tr = 'current';
        } elseif ($s === InventoryStockTransferStatus::Completed) {
            $ap = 'done';
            $tr = 'done';
            $cm = 'done';
        }

        return [
            ['label' => __('operational_ui.timeline.xfer.approval'), 'state' => $ap, 'kind' => 'approval'],
            ['label' => __('operational_ui.timeline.xfer.movement'), 'state' => $tr, 'kind' => 'fulfillment'],
            ['label' => __('operational_ui.timeline.xfer.completion'), 'state' => $cm, 'kind' => 'posting'],
        ];
    }

    private function blockedHtml(?InventoryStockTransferStatus $s): ?string
    {
        if ($s !== InventoryStockTransferStatus::Cancelled) {
            return null;
        }

        return View::make('components.ui.operational.blocked-alert', [
            'title' => __('operational_ui.blocked.xfer_cancelled_title'),
            'body' => __('operational_ui.blocked.xfer_cancelled_body'),
            'variant' => 'warning',
        ])->render();
    }

    /**
     * @return array{0: ?string, 1: ?string, 2: string}
     */
    private function nextTriple(?InventoryStockTransferStatus $s): array
    {
        return match ($s) {
            InventoryStockTransferStatus::Draft => [
                __('operational_ui.next.xfer_approve_title'),
                __('operational_ui.next.xfer_approve_body'),
                'info',
            ],
            InventoryStockTransferStatus::Approved,
            InventoryStockTransferStatus::InTransit => [
                __('operational_ui.next.xfer_execute_title'),
                __('operational_ui.next.xfer_execute_body'),
                'info',
            ],
            default => [null, null, 'info'],
        };
    }

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

        return $rows;
    }

    private function statusVariant(?InventoryStockTransferStatus $s): string
    {
        return match ($s) {
            InventoryStockTransferStatus::Draft => 'warning',
            InventoryStockTransferStatus::Approved => 'info',
            InventoryStockTransferStatus::InTransit => 'warning',
            InventoryStockTransferStatus::Completed => 'success',
            InventoryStockTransferStatus::Cancelled => 'danger',
            default => 'neutral',
        };
    }
}
