<?php

namespace App\Filament\Resources\UserResource\Pages;

use App\Filament\Resources\UserResource;
use App\Filament\Resources\UserResource\Widgets\MissedNotificationsOverview;
use App\Models\User;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\EditRecord;

class EditUser extends EditRecord
{
    protected static string $resource = UserResource::class;

    protected function getHeaderWidgets(): array
    {
        return [
            MissedNotificationsOverview::class,
        ];
    }

    public function getHeaderWidgetsColumns(): int|array
    {
        return 3;
    }

    protected function getHeaderActions(): array
    {
        return [];
    }

    protected function afterSave(): void
    {
        /** @var User $user */
        $user = $this->record;

        $tier = $this->data['tier'] ?? null;
        $cadence = $this->data['cadence'] ?? 'monthly';

        if ($tier === null) {
            return;
        }

        try {
            UserResource::applyTier($user, $tier, $cadence);
        } catch (\RuntimeException $e) {
            Notification::make()
                ->title('Tier not changed')
                ->body($e->getMessage())
                ->warning()
                ->send();
        }
    }
}
