<?php

namespace App\Filament\Resources\OrderResource\Widgets;

use Filament\Tables;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget as BaseWidget;
use Illuminate\Database\Eloquent\Model;

class OrderItemsTable extends BaseWidget
{
    public ?Model $record = null;

    protected static ?string $heading = 'Order Items';

    public function table(Table $table): Table
    {
        return $table
            ->query(
                fn () => $this->record->items()->getQuery()
            )
            ->columns([
                Tables\Columns\TextColumn::make('product.name')
                    ->label('Product'),
                Tables\Columns\TextColumn::make('printStock.name')
                    ->label('Print Stock')
                    ->default('N/A'),
                Tables\Columns\TextColumn::make('type')
                    ->badge(),
                Tables\Columns\TextColumn::make('specifications')
                    ->label('Specifications')
                    ->getStateUsing(function ($record) {
                        if ($record->type === 'wallpaper') {
                            return "Length: {$record->length}m";
                        } elseif ($record->type === 'mural') {
                            $area = number_format($record->width * $record->height, 2);
                            return "{$record->width}m × {$record->height}m ({$area}m²)";
                        }
                        return 'N/A';
                    }),
                Tables\Columns\TextColumn::make('price')
                    ->money('ZAR')
                    ->alignEnd(),
            ])
            ->paginated(false);
    }
}
