<?php

namespace App\Filament\Resources\CreditProducts\Tables;

use App\Enums\CreditProductType;
use App\Filament\Resources\CreditProducts\CreditProductResource;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TernaryFilter;
use Filament\Tables\Table;

class CreditProductsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->defaultSort('sort_order')
            ->columns([
                TextColumn::make('name')
                    ->label(__('admin.credit_products.fields.name'))
                    ->searchable()
                    ->sortable(),
                TextColumn::make('type')
                    ->label(__('admin.credit_products.fields.type'))
                    ->badge()
                    ->sortable(),
                TextColumn::make('credits')
                    ->label(__('admin.credit_products.fields.credits'))
                    ->numeric(decimalPlaces: 0)
                    ->sortable(),
                TextColumn::make('amount')
                    ->label(__('admin.credit_products.fields.amount'))
                    ->money(fn ($record): string => $record->currency, divideBy: 100)
                    ->sortable(),
                TextColumn::make('stripe_price_id')
                    ->label(__('admin.credit_products.fields.stripe_price_id'))
                    ->copyable()
                    ->searchable()
                    ->toggleable(),
                IconColumn::make('is_active')
                    ->label(__('admin.credit_products.fields.is_active'))
                    ->boolean()
                    ->sortable(),
                TextColumn::make('sort_order')
                    ->label(__('admin.credit_products.fields.sort_order'))
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('created_at')
                    ->label(__('admin.credit_products.fields.created_at'))
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                SelectFilter::make('type')
                    ->label(__('admin.credit_products.fields.type'))
                    ->options(CreditProductType::class),
                TernaryFilter::make('is_active')
                    ->label(__('admin.credit_products.fields.is_active')),
            ])
            ->recordActions([
                CreditProductResource::syncStripeAction(),
                EditAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }
}
