<?php

namespace App\Filament\Resources;

use App\Enums\FuelType;
use App\Enums\PlanTier;
use App\Filament\NavigationGroup;
use App\Filament\Resources\UserResource\Pages\EditUser;
use App\Filament\Resources\UserResource\Pages\ListUsers;
use App\Models\Plan;
use App\Models\User;
use Filament\Actions\DeleteAction;
use Filament\Actions\EditAction;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Resources\Resource;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\TernaryFilter;
use Filament\Tables\Table;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;

class UserResource extends Resource
{
    protected static ?string $model = User::class;

    protected static string|\UnitEnum|null $navigationGroup = NavigationGroup::Users;

    protected static ?int $navigationSort = 1;

    public static function form(Schema $schema): Schema
    {
        return $schema->components([
            Section::make('Profile')->columns(2)->schema([
                TextInput::make('name')
                    ->required()
                    ->maxLength(255),
                TextInput::make('email')
                    ->email()
                    ->required()
                    ->maxLength(255),
                TextInput::make('postcode')
                    ->maxLength(8),
                Select::make('preferred_fuel_type')
                    ->label('Preferred fuel type')
                    ->options(collect(FuelType::cases())
                        ->mapWithKeys(fn (FuelType $t) => [$t->value => $t->label()])
                        ->all())
                    ->required(),
            ]),

            Section::make('Access')->columns(2)->schema([
                Toggle::make('is_admin')
                    ->label('Admin')
                    ->helperText('Grants access to this admin panel.'),
                DateTimePicker::make('email_verified_at')
                    ->label('Email verified at'),
            ]),

            Section::make('Subscription')->columns(2)->schema([
                Select::make('tier')
                    ->label('Tier')
                    ->options([
                        PlanTier::Free->value => 'Free',
                        PlanTier::Basic->value => 'Basic',
                        PlanTier::Plus->value => 'Plus',
                        PlanTier::Pro->value => 'Pro',
                    ])
                    ->required()
                    ->live()
                    ->dehydrated(false)
                    ->afterStateHydrated(fn (Select $component, ?User $record) => $component
                        ->state($record ? Plan::resolveForUser($record)->name : PlanTier::Free->value)),
                Select::make('cadence')
                    ->label('Billing Cadence')
                    ->options([
                        'monthly' => 'Monthly',
                        'annual' => 'Annual',
                    ])
                    ->default('monthly')
                    ->dehydrated(false)
                    ->visible(fn (callable $get): bool => ($get('tier') ?? '') !== PlanTier::Free->value)
                    ->helperText('Only applies when assigning a paid tier. Real Stripe subscriptions are not modified.'),
            ]),

            Section::make('Security')->columns(2)->schema([
                DateTimePicker::make('two_factor_confirmed_at')
                    ->label('2FA confirmed at')
                    ->disabled(),
                TextInput::make('password')
                    ->label('Set new password')
                    ->password()
                    ->revealable()
                    ->minLength(8)
                    ->dehydrated(fn (?string $state): bool => filled($state))
                    ->dehydrateStateUsing(fn (string $state): string => bcrypt($state))
                    ->helperText('Leave blank to keep current password.')
                    ->afterStateHydrated(fn (TextInput $component) => $component->state(null)),
            ]),

            Section::make('Billing')->columns(3)->schema([
                TextInput::make('stripe_id')
                    ->label('Stripe customer ID')
                    ->disabled(),
                TextInput::make('pm_type')
                    ->label('Payment method')
                    ->disabled(),
                TextInput::make('pm_last_four')
                    ->label('Card last 4')
                    ->disabled(),
            ]),

            Section::make('Timestamps')->columns(2)->schema([
                DateTimePicker::make('created_at')->disabled(),
                DateTimePicker::make('updated_at')->disabled(),
            ]),
        ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('name')->searchable()->sortable(),
                TextColumn::make('email')->searchable()->sortable(),
                TextColumn::make('postcode')->placeholder('—'),
                TextColumn::make('tier')
                    ->label('Tier')
                    ->state(fn (User $record): string => Plan::resolveForUser($record)->name)
                    ->badge()
                    ->colors([
                        'gray' => 'free',
                        'primary' => 'basic',
                        'warning' => 'plus',
                        'success' => 'pro',
                    ]),
                IconColumn::make('is_admin')
                    ->label('Admin')
                    ->boolean(),
                TextColumn::make('created_at')
                    ->dateTime('d M Y')
                    ->sortable(),
            ])
            ->defaultSort('created_at', 'desc')
            ->filters([
                TernaryFilter::make('is_admin')
                    ->label('Admins only'),
            ])
            ->recordActions([
                EditAction::make(),
                DeleteAction::make(),
            ]);
    }

    /**
     * Cancel any existing admin-granted subscription, then (if a paid tier
     * was requested) insert a fresh synthetic active subscription row.
     */
    public static function applyTier(User $user, string $tier, string $cadence): void
    {
        $hasRealStripeSubscription = $user->subscriptions()
            ->where('stripe_id', 'not like', 'admin_%')
            ->whereIn('stripe_status', ['active', 'trialing', 'past_due'])
            ->exists();

        if ($hasRealStripeSubscription) {
            throw new \RuntimeException(
                "User {$user->email} has an active Stripe subscription — modify it through the Stripe dashboard, not the admin panel."
            );
        }

        $user->subscriptions()->where('stripe_id', 'like', 'admin_%')->delete();

        if ($tier === PlanTier::Free->value) {
            self::bustPlanCache($user);

            return;
        }

        $priceId = config("services.stripe.prices.{$tier}.{$cadence}") ?? "price_admin_{$tier}_{$cadence}";

        $user->subscriptions()->create([
            'type' => 'default',
            'stripe_id' => 'admin_'.Str::uuid(),
            'stripe_status' => 'active',
            'stripe_price' => $priceId,
            'quantity' => 1,
        ]);

        self::bustPlanCache($user);
    }

    protected static function bustPlanCache(User $user): void
    {
        if (Cache::supportsTags()) {
            Cache::tags(['plans'])->flush();
        } else {
            Cache::forget("plan_for_user_{$user->id}");
        }
    }

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