<?php

declare(strict_types=1);


namespace Plugins\Neatstudio\Membership\Filament\Resources;

use BackedEnum;
use Filament\Forms\Components\KeyValue;
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\Table;
use Plugins\Neatstudio\Membership\Filament\Resources\Pages\ListMembershipPlans;
use Plugins\Neatstudio\Membership\Filament\Resources\Pages\EditMembershipPlan;
use Plugins\Neatstudio\Membership\Filament\Resources\Pages\CreateMembershipPlan;
use Plugins\Neatstudio\Membership\Models\MembershipPlan;

class MembershipPlanResource extends Resource
{
    protected static \UnitEnum|string|null $navigationGroup = '管理';

    protected static ?string $model = MembershipPlan::class;

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

    protected static ?string $navigationLabel = '会员套餐';

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                TextInput::make('name')->label('套餐名')->required(),
                TextInput::make('slug')->label('Slug')->required()->unique(ignoreRecord: true),
                Textarea::make('description')->label('描述')->rows(2)->columnSpanFull(),
                TextInput::make('price')->label('价格（分）')->numeric()->required()->default(0)->helperText('例：9900 = ¥99'),
                TextInput::make('duration_days')->label('时长（天）')->numeric()->required()->default(30),
                KeyValue::make('permissions')->label('权限位')->columnSpanFull(),
                Toggle::make('active')->label('上架')->default(true),
                TextInput::make('sort')->label('排序')->numeric()->default(0),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                \Filament\Tables\Columns\TextColumn::make('name')->label('套餐'),
                \Filament\Tables\Columns\TextColumn::make('price')->label('价格')
                    ->formatStateUsing(fn ($state) => '¥'.number_format((float) $state / 100, 2)),
                \Filament\Tables\Columns\TextColumn::make('duration_days')->label('时长')->suffix(' 天'),
                \Filament\Tables\Columns\IconColumn::make('active')->label('上架')->boolean(),
                \Filament\Tables\Columns\TextColumn::make('sort')->label('排序')->sortable(),
            ])
            ->filters([])
            ->recordActions([
                \Filament\Actions\EditAction::make(),
            ])
            ->toolbarActions([
                \Filament\Actions\BulkActionGroup::make([
                    \Filament\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

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