<?php

namespace App\Livewire\Studio\Manage\Order;

use App\Models\Order;
use App\Models\OrderItem;
use App\Models\Payment;
use Livewire\Attributes\Title;
use Livewire\Component;

#[Title('Detail Order')]
class Show extends Component
{
    public array $orderData = [];

    public array $items = [];

    public array $payments = [];

    public function mount(Order $order)
    {
        $order->load(['items', 'payments']);

        $this->orderData = [
            'customer' => $order->customer?->name ?? 'Anonim',
            'invoice_number' => $order->invoice_number,
            'status' => $order->status->label(),
            'date' => formatDateTime($order->created_at),
            'channel' => $order->channel->label(),
            'subtotal' => currency($order->subtotal, 'Rp'),
            'discount' => currency($order->discount, 'Rp'),
            'total' => currency($order->total, 'Rp'),
        ];

        $this->items = $order->items
            ->map(fn (OrderItem $item) => [
                'id' => $item->hash,
                'name' => $item->orderable?->name,
                'quantity' => $item->quantity,
                'unit_price' => currency($item->unit_price, 'Rp'),
                'total' => currency($item->quantity * $item->unit_price, 'Rp'),
            ])
            ->toArray();

        $this->payments = $order->payments->map(fn (Payment $payment) => [
            'id' => $payment->id,
        ])
            ->toArray();
    }

    public function render()
    {
        return view('livewire.studio.manage.order.show', [
            'pageTitle' => 'Detail Order',
        ]);
    }
}
