<?php

namespace App\Filament\Resources;

use BackedEnum;
use Filament\Support\Icons\Heroicon;
use UnitEnum;
use App\Filament\Resources\PlanResource\Pages\CreatePlan;
use App\Filament\Resources\PlanResource\Pages\EditPlan;
use App\Filament\Resources\PlanResource\Pages\ListPlans;
use App\Models\Plan;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Actions\ViewAction;
use Filament\Forms\Components\KeyValue;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Resources\Resource;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\BooleanColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;

class PlanResource extends Resource
{
    protected static ?string $model = Plan::class;

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

    protected static string|UnitEnum|null $navigationGroup = 'Admin';

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make('Plan Information')
                    ->description('Add a new plan')
                    ->schema([
                        TextInput::make('name')->label('Page Name')
                            ->required(),
                        TextInput::make('description'),
                        TextInput::make('product_id')->required(),
                        TextInput::make('pricing_id')->required(),
                        TextInput::make('shoppy_product_id')->nullable(),
                        TextInput::make('oxapay_link')->nullable(),
                        TextInput::make('price')->numeric()->required(),
                        TextInput::make('mailbox_limit')->numeric()->required(),
                        Select::make('monthly_billing')->options([
                            1 => 'Monthly',
                            0 => 'Yearly',
                        ])->required(),
                        Select::make('accept_stripe')->options([
                            1 => 'Activate',
                            0 => 'Disable',
                        ])->required(),
                        Select::make('accept_shoppy')->options([
                            1 => 'Activate',
                            0 => 'Disable',
                        ])->required(),
                        Select::make('accept_oxapay')->options([
                            1 => 'Activate',
                            0 => 'Disable',
                        ])->required(),
                        KeyValue::make('details')
                            ->label('Plan Details (Optional)')
                            ->keyPlaceholder('Name')
                            ->valuePlaceholder('Content')
                            ->reorderable(),
                    ]),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('name')->label('Name')->searchable(),
                TextColumn::make('product_id')->label('Product'),
                TextColumn::make('pricing_id')->label('Pricing'),
                TextColumn::make('price')->label('Price'),
                BooleanColumn::make('monthly_billing')->label('Monthly Billing'),
            ])
            ->searchable()
            ->filters([
                SelectFilter::make('payment_method')
                    ->label('Payment Method')
                    ->options([
                        'stripe' => 'Stripe',
                        'shoppy' => 'Shoppy',
                        'oxapay' => 'OxaPay',
                    ])
                    ->query(function ($query, array $data) {
                        if (isset($data['value'])) {
                            if ($data['value'] === 'stripe') {
                                return $query->where('accept_stripe', true);
                            }
                            if ($data['value'] === 'shoppy') {
                                return $query->where('accept_shoppy', true);
                            }
                            if ($data['value'] === 'oxapay') {
                                return $query->where('accept_oxapay', true);
                            }
                        }

                        return $query;
                    }),
            ])
            ->recordActions([
                ViewAction::make(),
                EditAction::make(),
                DeleteAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => ListPlans::route('/'),
            'create' => CreatePlan::route('/create'),
            'edit' => EditPlan::route('/{record}/edit'),
        ];
    }
}
