<?php

namespace App\Filament\Resources;

use App\Filament\Clusters\WeeklyOps\WeeklyOpsCluster;
use App\Filament\Resources\TenantPackageResource\Pages;
use App\Models\TenantPackage;
use App\Services\Audit\SuperAdminAuditLogger;
use BackedEnum;
use Filament\Actions\ActionGroup;
use Filament\Actions\BulkActionGroup;
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\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Collection;

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

    protected static ?string $cluster = WeeklyOpsCluster::class;

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

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

    public static function getNavigationGroup(): string
    {
        return __('admin.nav.commercial');
    }

    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()
                        ->after(fn (array $data, TenantPackage $record) => app(SuperAdminAuditLogger::class)->recordModelMutation(
                            'updated',
                            $record,
                            SuperAdminAuditLogger::fieldsMetadata($data),
                            static::class
                        )),
                    DeleteAction::make()
                        ->after(fn (TenantPackage $record) => app(SuperAdminAuditLogger::class)->recordModelMutation(
                            'deleted',
                            $record,
                            source: static::class
                        )),
                ]),
            ])
            ->bulkActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make()
                        ->after(function (Collection $records): void {
                            $logger = app(SuperAdminAuditLogger::class);

                            foreach ($records as $record) {
                                $logger->recordModelMutation(
                                    'deleted',
                                    $record,
                                    source: static::class
                                );
                            }
                        }),
                ]),
            ]);
    }

    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'),
        ];
    }
}
