<?php

declare(strict_types=1);

namespace App\Filament\Components\Table;

use Filament\Tables\Columns\TextColumn;

class QuantityTextColumn extends TextColumn
{
    public static function create(): static
    {
        return parent::make('quantity')
            ->label('تعداد')
            ->sortable()
            ->getStateUsing(function ($record) {
                // Get the total quantity by summing up all variant quantities
                $totalQuantity = $record->variants->sum('quantity');

                // Format the number with Persian digits
                $formatted = number_format(
                    (float) $totalQuantity,
                    0,
                    '/',
                    '،'
                );

                // Convert to Persian numbers
                $persianNumber = str_replace(
                    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
                    ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'],
                    $formatted
                );

                return '<div style="text-align: left;">' . $persianNumber . '</div>';
            })
            ->html() // Allow HTML
            ->toggleable();
    }
}
