<?php
/*
    * Created by VahabDev in Aparat Live
    * Website: https://pourfi.ir
    * Aparat Live: https://www.aparat.com/vahabdev/live
    * Aparat Channel: https://www.aparat.com/vahabdev
    * Rubika Channel: https://rubika.ir/vahabdev
    * Donate: https://reymit.ir/vahabdev
    * Telegram: https://t.me/vahabdev
*/


namespace App\Filament\Resources\Comments\Tables;

use App\Models\Blog;
use App\Models\Product;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

class CommentsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('name')
                    ->label('نام کاربر')
                    ->searchable(),

                TextColumn::make('post_type')
                    ->label('نوع')
                    ->formatStateUsing(fn (string $state): string => match ($state) {
                        'blog' => 'وبلاگ',
                        'product' => 'محصول',
                    })
                    ->badge()
                    ->color(fn (string $state): string => match ($state) {
                        'blog' => 'info',
                        'product' => 'warning',
                    }),

                // نمایش نام پست یا محصول مرتبط
                TextColumn::make('relid')
                    ->label('مربوط به')
                    ->formatStateUsing(function ($state, $record) {
                        if ($record->post_type === 'blog') {
                            $blog = Blog::find($state);
                            return $blog ? $blog->title : '-';
                        } elseif ($record->post_type === 'product') {
                            $product = Product::find($state);
                            return $product ? $product->name : '-';
                        }
                        return '-';
                    })
                    ->searchable(),

                TextColumn::make('comment')
                    ->label('نظر')
                    ->limit(50) // نمایش ۵۰ کاراکتر اول
                    ->wrap(),

                TextColumn::make('status')
                    ->label('وضعیت')
                    ->badge()
                    ->formatStateUsing(fn ($record) => $record->status_label)
                    ->color(fn (string $state): string => match ($state) {
                        'active' => 'success',
                        'pending' => 'warning',
                    }),

                TextColumn::make('created_at')
                    ->label('تاریخ')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                //
            ])
            ->recordActions([
                EditAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }
}
