<?php

namespace App\Filament\Resources\ResellerOrders\RelationManagers;

use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

class OrderHistoriesRelationManager extends RelationManager
{
    protected static string $relationship = 'histories';

    protected static ?string $title = 'Activity';

    protected static ?string $modelLabel = 'activity';

    public function isReadOnly(): bool
    {
        return true;
    }

    public function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('action')
            ->defaultSort('created_at', 'desc')
            ->columns([
                TextColumn::make('created_at')
                    ->label('Time')
                    ->dateTime('M j, Y H:i')
                    ->sortable()
                    ->since(),
                TextColumn::make('action')
                    ->label('Event')
                    ->formatStateUsing(fn (string $state): string => match ($state) {
                        'status_changed'   => 'Status Changed',
                        'payment_updated'  => 'Payment Updated',
                        'order_created'    => 'Order Created',
                        'note_added'       => 'Note Added',
                        default            => ucwords(str_replace('_', ' ', $state)),
                    })
                    ->badge()
                    ->color(fn (string $state): string => match ($state) {
                        'status_changed'  => 'info',
                        'payment_updated' => 'warning',
                        'order_created'   => 'success',
                        default           => 'gray',
                    }),
                TextColumn::make('from_status')
                    ->label('From')
                    ->formatStateUsing(fn (?string $state): string => $state ? ucfirst($state) : '—')
                    ->badge()
                    ->color(fn (?string $state): string => match ($state) {
                        'pending'    => 'info',
                        'processing' => 'warning',
                        'shipped'    => 'info',
                        'delivered'  => 'success',
                        'cancelled'  => 'danger',
                        default      => 'gray',
                    }),
                TextColumn::make('to_status')
                    ->label('To')
                    ->formatStateUsing(fn (?string $state): string => $state ? ucfirst($state) : '—')
                    ->badge()
                    ->color(fn (?string $state): string => match ($state) {
                        'pending'    => 'info',
                        'processing' => 'warning',
                        'shipped'    => 'info',
                        'delivered'  => 'success',
                        'cancelled'  => 'danger',
                        default      => 'gray',
                    }),
                TextColumn::make('notes')
                    ->label('Notes')
                    ->wrap()
                    ->limit(80),
                TextColumn::make('user.name')
                    ->label('By')
                    ->default('System'),
            ])
            ->filters([])
            ->headerActions([])
            ->recordActions([])
            ->toolbarActions([]);
    }
}
