<?php

namespace App\Filament\Resources\SupplierResource\RelationManagers;

use App\Models\PurchaseOrder;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;

class PurchaseOrdersRelationManager extends RelationManager
{
    protected static string $relationship = 'purchaseOrders';

    protected static ?string $title = 'أوامر الشراء';

    protected static ?string $modelLabel = 'أمر شراء';

    public function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('id')
            ->columns([
                Tables\Columns\TextColumn::make('id')
                    ->label('#'),

                Tables\Columns\TextColumn::make('status')
                    ->label('الحالة')
                    ->badge()
                    ->formatStateUsing(fn (string $state) => PurchaseOrder::getStatusLabels()[$state] ?? $state)
                    ->color(fn (string $state): string => match ($state) {
                        'draft' => 'gray',
                        'confirmed' => 'warning',
                        'received' => 'success',
                        default => 'gray',
                    }),

                Tables\Columns\TextColumn::make('total_cost')
                    ->label('الإجمالي (ج.م)')
                    ->money('EGP'),

                Tables\Columns\TextColumn::make('sourceOrder.id')
                    ->label('طلب مرتبط')
                    ->placeholder('—'),

                Tables\Columns\TextColumn::make('created_at')
                    ->label('التاريخ')
                    ->dateTime('d/m/Y'),
            ])
            ->defaultSort('id', 'desc');
    }
}
