<?php

namespace App\Support\Operational;

use App\Modules\Sales\Models\SalesReturn;
use App\Modules\Sales\Support\SalesReturnStatus;
use Illuminate\Support\Facades\View;

final class SalesReturnOperationalPresenter
{
    /**
     * @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(SalesReturn $ret): array
    {
        $s = SalesReturnStatus::tryFrom((string) $ret->status);
        [$nt, $nb, $nv] = $this->nextTriple($s);

        return [
            'timelineSteps' => $this->timelineSteps($s),
            'auditRows' => $this->auditRows($ret),
            '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(?SalesReturnStatus $s): array
    {
        $ap = 'pending';
        $rc = 'pending';
        $pr = 'pending';
        if ($s === SalesReturnStatus::Rejected) {
            $ap = 'current';
        }
        if ($s === SalesReturnStatus::Draft) {
            $ap = 'current';
        } elseif ($s === SalesReturnStatus::Approved) {
            $ap = 'done';
            $rc = 'current';
        } elseif ($s === SalesReturnStatus::Received) {
            $ap = 'done';
            $rc = 'done';
            $pr = 'current';
        } elseif ($s === SalesReturnStatus::Processed) {
            $ap = 'done';
            $rc = 'done';
            $pr = 'done';
        }

        $metaApproval = match ($s) {
            SalesReturnStatus::Rejected => __('operational_ui.timeline.meta.rejected'),
            SalesReturnStatus::Draft => __('operational_ui.timeline.meta.pending'),
            default => null,
        };

        return [
            ['label' => __('operational_ui.timeline.sr.approval'), 'state' => $ap, 'kind' => 'approval', 'meta' => $metaApproval],
            ['label' => __('operational_ui.timeline.sr.receive'), 'state' => $rc, 'kind' => 'fulfillment'],
            ['label' => __('operational_ui.timeline.sr.processing'), 'state' => $pr, 'kind' => 'posting'],
        ];
    }

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

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

    /**
     * @return array{0: ?string, 1: ?string, 2: string}
     */
    private function nextTriple(?SalesReturnStatus $s): array
    {
        return match ($s) {
            SalesReturnStatus::Draft => [
                __('operational_ui.next.sr_approve_title'),
                __('operational_ui.next.sr_approve_body'),
                'info',
            ],
            SalesReturnStatus::Approved => [
                __('operational_ui.next.sr_receive_title'),
                __('operational_ui.next.sr_receive_body'),
                'info',
            ],
            SalesReturnStatus::Received => [
                __('operational_ui.next.sr_process_title'),
                __('operational_ui.next.sr_process_body'),
                'warning',
            ],
            default => [null, null, 'info'],
        };
    }

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

        return $rows;
    }

    private function statusVariant(?SalesReturnStatus $s): string
    {
        return match ($s) {
            SalesReturnStatus::Draft => 'warning',
            SalesReturnStatus::Approved => 'info',
            SalesReturnStatus::Received => 'warning',
            SalesReturnStatus::Processed => 'success',
            SalesReturnStatus::Rejected => 'danger',
            default => 'neutral',
        };
    }
}
