<?php

namespace App\Filament\Pages\Auth;

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 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 getHeading(): string|Htmlable
    {
        return '';
    }

    public function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                TextInput::make('name')
                    ->label('Nama')
                    ->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()
                    ->aboveContent('Pastikan alamat surel aktif, karena akan digunakan 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'),

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

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

        CheerfulNotification::success(
            'Selamat Datang! 👋🎉',
            "Halooo, {$user->name}!, terima kasih sudah bergabung! Akunmu berhasil dibuat. Silakan verifikasi alamat surelmu! 🚀✨"
        )
            ->send();

        $this->getUserModel()::superAdmin()
            ->get()
            ->each(function ($admin) use ($user): void {
                $admin->notify(new BroadcastNotification([
                    'title' => 'Warga Baru Nih! 👋🎉',
                    'body' => "Halo Admin! {$user->name} baru saja mendaftar. Yuk cek kelengkapannya! 🚀",
                    'action' => [
                        Action::make('view')
                            ->label('Lihat')
                            ->url(PartnerResource::getUrl('view', ['record' => $user->id])),
                    ],
                ]));
            });

        return $user;
    }
}
