<?php

namespace App\Filament\Pages\Auth;

use App\Enums\RoleEnum;
use App\Filament\Resources\Manage\Partners\PartnerResource;
use App\Filament\Support\CheerfulNotification;
use App\Notifications\BroadcastNotification;
use Filament\Actions\Action;
use Filament\Auth\Pages\Register;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Schema;
use Filament\Support\Enums\Width;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules\Password;

class Registration extends Register
{
    public function getMaxContentWidth(): Width
    {
        return Width::ExtraLarge;
    }

    public function getHeading(): string|Htmlable
    {
        return '';
    }

    public function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                TextInput::make('name')
                    ->label('Nama Lengkap')
                    ->placeholder('John Doe')
                    ->autocomplete(false)
                    ->autofocus()
                    ->required()
                    ->maxLength(100),

                TextInput::make('email')
                    ->label('Alamat Surel')
                    ->placeholder('johndoe@example.com')
                    ->autocomplete(false)
                    ->required()
                    ->maxLength(254)
                    ->unique('users', 'email')
                    ->email()
                    ->prefixIcon('heroicon-o-envelope')
                    ->aboveContent('Pastikan alamat surel aktif untuk verifikasi akun Anda.'),

                TextInput::make('username')
                    ->label('Nama Pengguna')
                    ->placeholder('johndoe')
                    ->autocomplete(false)
                    ->required()
                    ->minLength(5)
                    ->maxLength(10)
                    ->regex('/^[a-zA-Z0-9_.-]+$/')
                    ->unique('users', 'username')
                    ->prefixIcon('heroicon-o-at-symbol'),

                TextInput::make('password')
                    ->label('Kata Sandi')
                    ->password()
                    ->revealable(filament()->arePasswordsRevealable())
                    ->required()
                    ->rule(Password::default())
                    ->autocomplete(false)
                    ->dehydrated(fn ($state): bool => filled($state))
                    ->dehydrateStateUsing(fn ($state): string => Hash::make($state))
                    ->placeholder('********')
                    ->prefixIcon('heroicon-o-lock-closed'),
            ]);
    }

    protected function handleRegistration(array $data): Model
    {
        $user = $this->getUserModel()::create($data);

        $user->assignRole(RoleEnum::PERUSAHAAN->value);

        CheerfulNotification::success(
            CheerfulNotification::getByKey('auth.registration.success.title'),
            CheerfulNotification::getByKey('auth.registration.success.body', ['name' => $user->name])
        )
            ->send();

        $this->getUserModel()::superAdmin()
            ->get()
            ->each(function ($admin) use ($user): void {
                $admin->notify(new BroadcastNotification([
                    'title' => CheerfulNotification::getByKey('auth.registration.admin_notify.title'),
                    'body' => CheerfulNotification::getByKey('auth.registration.admin_notify.body', ['name' => $user->name]),
                    'action' => [
                        Action::make('view')
                            ->label('Lihat')
                            ->url(PartnerResource::getUrl('view', ['record' => $user->id])),
                    ],
                ]));
            });

        return $user;
    }
}
