<?php

namespace App\Filament\Resources;

use App\Filament\Resources\TenantPackageResource\Pages;
use App\Models\TenantPackage;
use BackedEnum;
use Filament\Actions\ActionGroup;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\CreateAction;
use Filament\Actions\DeleteAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Actions\ViewAction;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use UnitEnum;

class TenantPackageResource extends Resource
{
    protected static ?string $model = TenantPackage::class;

    protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-shopping-bag';

    protected static ?string $slug = 'tenant-packages';

    public static function getNavigationGroup(): string
    {
        return 'Billing & Finanzen';
    }

    public static function form(Schema $form): Schema
    {
        return $form
            ->schema([
                Select::make('tenant_id')
                    ->relationship('tenant', 'name')
                    ->required()
                    ->searchable(),
                Select::make('package_id')
                    ->relationship('package', 'name')
                    ->required()
                    ->searchable(),
                DateTimePicker::make('purchased_at'),
                DateTimePicker::make('expires_at'),
                Toggle::make('active')->default(true),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('tenant.name')->searchable()->sortable(),
                TextColumn::make('package.name')->badge()->color('success'),
                TextColumn::make('purchased_at')->dateTime()->sortable(),
                TextColumn::make('expires_at')->dateTime()->sortable(),
                IconColumn::make('active')->boolean(),
            ])
            ->filters([])
            ->actions([
                ActionGroup::make([
                    ViewAction::make(),
                    EditAction::make(),
                    DeleteAction::make(),
                ]),
            ])
            ->bulkActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }

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

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListTenantPackages::route('/'),
            'create' => Pages\CreateTenantPackage::route('/create'),
            'view' => Pages\ViewTenantPackage::route('/{record}'),
            'edit' => Pages\EditTenantPackage::route('/{record}/edit'),
        ];
    }
}
