<?php

namespace App\Nova\Resources\Ecommerce\Product\Order\OrderItem;

use App\Repositories\Nova\NovaRepo;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\URL;

class OrderItemFieldsForIndex
{
    /**
     * Order item's index fields
     */
    public static function make($resource): array
    {
        return [
            ID::make()->sortable()->hidden(),

            Text::make(
                name: __('Image'),
                attribute: fn () => NovaRepo::rawImage(url: $resource->product?->thumbnail())
            )->asHtml(),

            Text::make(
                name: __('Product name'),
                attribute: fn () => $resource->product?->novaDetailPage(),
            )->displayUsing(function () use ($resource) {
                $product = $resource->product;

                if (! $product) {
                    return;
                }

                return sprintf('
                    <a href="%s" target="_blank" class="link-default"> %s <span class="link-default">%s</span> <span class="link-default">%s</span></a>',
                    $product->novaDetailPage(),
                    $product->name,
                    $product->color,
                    $product->size,
                );
            })->asHtml(),

            URL::make(
                name: __('Channel'),
                attribute: fn () => $resource->channel?->novaDetailPage(),
            )
                ->displayUsing(fn () => $resource->channel?->name)
                ->canSeeWhen('isAdmin', $resource),

            Text::make(__('Cost amount'), fn () => $resource->product?->cost_amount),
            Text::make(__('Price'), 'unit_price_amount'),
            Text::make(__('Quantity'), 'quantity'),
            Text::make(__('Total'), fn () => intval($resource->quantity) * floatval($resource->unit_price_amount)),
        ];
    }
}
