<?php

namespace App\Filament\Resources\Products;

use App\Filament\Resources\Products\Pages\ManageProducts;
use App\Models\Product;
use BackedEnum;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\ImageColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;

class ProductResource extends Resource
{
    protected static ?string $model = Product::class;

    protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedShoppingBag;

    protected static ?string $navigationLabel = 'Harytlar';

    protected static string|\UnitEnum|null $navigationGroup = 'Harytlar';

    protected static ?int $navigationSort = 2;

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Select::make('category_id')
                    ->label('Kategoriýa')
                    ->relationship('category', 'name')
                    ->required()
                    ->searchable()
                    ->preload(),
                TextInput::make('name')
                    ->label('Ady')
                    ->required(),
                TextInput::make('slug')
                    ->label('Slug')
                    ->required()
                    ->unique(ignoreRecord: true),
                TextInput::make('price')
                    ->label('Bahasy')
                    ->required()
                    ->numeric()
                    ->prefix('TMT'),
                TextInput::make('price_unit')
                    ->label('Baha birligi')
                    ->required()
                    ->default('kg')
                    ->placeholder('kg, sany, gram'),
                Select::make('availability_status')
                    ->label('Elýeterliligi')
                    ->required()
                    ->options([
                        'available' => 'Elýeterli',
                        'out_of_season' => 'Möwsümden daşary',
                        'low_stock' => 'Az galdy',
                    ])
                    ->default('available'),
                Textarea::make('description')
                    ->label('Beýany')
                    ->rows(3)
                    ->columnSpanFull(),
                FileUpload::make('image')
                    ->label('Surat')
                    ->image()
                    ->disk('public')
                    ->directory('products')
                    ->columnSpanFull(),
                TextInput::make('sort_order')
                    ->label('Tertip')
                    ->required()
                    ->numeric()
                    ->default(0),
                Toggle::make('is_active')
                    ->label('Işjeň')
                    ->default(true),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                ImageColumn::make('image')
                    ->label('Surat')
                    ->disk('public')
                    ->circular(),

                TextColumn::make('name')
                    ->label('Ady')
                    ->searchable(),
                TextColumn::make('category.name')
                    ->label('Kategoriýa')
                    ->badge()
                    ->sortable(),
                TextColumn::make('price')
                    ->label('Bahasy')
                    ->formatStateUsing(fn ($state) => number_format($state, 2).' TMT')
                    ->sortable(),
                TextColumn::make('price_unit')
                    ->label('Birlik'),
                TextColumn::make('availability_status')
                    ->label('Elýeterliligi')
                    ->badge()
                    ->color(fn (string $state): string => match ($state) {
                        'available' => 'success',
                        'out_of_season' => 'warning',
                        'low_stock' => 'danger',
                        default => 'gray',
                    })
                    ->formatStateUsing(fn (string $state): string => match ($state) {
                        'available' => 'Elýeterli',
                        'out_of_season' => 'Möwsümden daşary',
                        'low_stock' => 'Az galdy',
                        default => $state,
                    }),
                IconColumn::make('is_active')
                    ->label('Işjeň')
                    ->boolean(),
                TextColumn::make('sort_order')
                    ->label('Tertip')
                    ->numeric()
                    ->sortable(),
                TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->defaultSort('sort_order')
            ->filters([
                SelectFilter::make('category')
                    ->relationship('category', 'name')
                    ->label('Kategoriýa'),
                SelectFilter::make('availability_status')
                    ->options([
                        'available' => 'Elýeterli',
                        'out_of_season' => 'Möwsümden daşary',
                        'low_stock' => 'Az galdy',
                    ])
                    ->label('Elýeterliligi'),
            ])
            ->recordActions([
                EditAction::make(),
                DeleteAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getPages(): array
    {
        return [
            'index' => ManageProducts::route('/'),
        ];
    }
}
