<?php

namespace App\Livewire\Forms;

use App\Models\Application;
use Livewire\Attributes\Validate;
use Illuminate\Support\Str;
use Livewire\Component;

class NewApplication extends Component
{
    #[Validate('required')]
    public string $name = '';
    #[Validate('required|url')]
    public string $redirect_uri = '';

    public function create()
    {
        $this->authorize('create', Application::class);
        
        $this->validate();

        Application::create([
            'name' => $this->name,
            'redirect_uri' => $this->redirect_uri,
            'client_id' => Str::uuid()->toString(),
            'client_secret' => Str::random(40),
        ]);

        $this->reset(['name', 'redirect_uri']);
        return redirect(route('dashboard'));
    }

    public function render()
    {
        return view('livewire.forms.new-application');
    }
}
