<?php

namespace App\Filament\Resources\Products\Tables;

use App\Models\Product;
use Filament\Actions\Action;
use Filament\Actions\ActionGroup;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Notifications\Notification;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\SpatieMediaLibraryImageColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

class ProductsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                SpatieMediaLibraryImageColumn::make('images')
                    ->collection('images')
                    ->label('Image'),
                TextColumn::make('name')
                    ->searchable()
                    ->sortable()
                    ->limit(35),
                TextColumn::make('brand.name')
                    ->label('Brand')
                    ->searchable()
                    ->sortable()
                    ->toggleable(),
                IconColumn::make('is_visible')
                    ->label('Visibility')
                    ->boolean()
                    ->sortable(),
                TextColumn::make('price')
                    ->label('Price')
                    ->sortable(),
                TextColumn::make('sku')
                    ->label('SKU')
                    ->searchable()
                    ->sortable()
                    ->toggleable(),
                TextColumn::make('quantity')
                    ->label('Quantity')
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                // We can add filters in subsequent steps
            ])
            ->actions([
                ActionGroup::make([
                    EditAction::make()
                        ->icon('heroicon-o-pencil')
                        ->color('primary'),

                    Action::make('toggle_visibility')
                        ->label(fn (Product $record): string => $record->is_visible ? 'Hide' : 'Publish')
                        ->icon(fn (Product $record): string => $record->is_visible ? 'heroicon-o-eye-slash' : 'heroicon-o-eye')
                        ->color('gray')
                        ->action(function (Product $record): void {
                            $record->update(['is_visible' => !$record->is_visible]);

                            Notification::make()
                                ->title($record->is_visible ? 'Product published' : 'Product hidden')
                                ->success()
                                ->send();
                        }),

                    Action::make('adjust_price')
                        ->label('Adjust price')
                        ->icon('heroicon-o-currency-dollar')
                        ->color('warning')
                        ->form([
                            \Filament\Forms\Components\TextInput::make('price')
                                ->label('New price')
                                ->numeric()
                                ->required()
                                ->default(fn (Product $record) => $record->price),
                        ])
                        ->action(function (Product $record, array $data): void {
                            $record->update(['price' => $data['price']]);

                            Notification::make()
                                ->title('Price adjusted')
                                ->success()
                                ->send();
                        }),

                    Action::make('adjust_stock')
                        ->label('Adjust stock')
                        ->icon('heroicon-o-adjustments-horizontal')
                        ->color('info')
                        ->form([
                            \Filament\Forms\Components\TextInput::make('quantity')
                                ->label('New stock quantity')
                                ->numeric()
                                ->required()
                                ->default(fn (Product $record) => $record->quantity),
                        ])
                        ->action(function (Product $record, array $data): void {
                            $difference = $data['quantity'] - $record->quantity;
                            if ($difference !== 0) {
                                \App\Services\StockAdjustmentService::adjust($record, $difference, 'manual', [
                                    'notes' => 'Adjusted stock via table action.',
                                    'user_id' => auth()->id(),
                                ]);
                            }

                            Notification::make()
                                ->title('Stock adjusted')
                                ->success()
                                ->send();
                        }),

                    DeleteAction::make()
                        ->icon('heroicon-o-trash')
                        ->color('danger'),
                ])
                ->color('info'),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }
}
