<?php

use App\Enums\UserStatus;
use App\Enums\UserType;
use App\Models\AdminLog;
use App\Models\User;
use App\Notifications\WelcomeAccountNotification;
use Illuminate\Support\Facades\Hash;
use Livewire\Volt\Component;

new class extends Component {
    public string $company_name = '';
    public string $company_cert_number = '';
    public string $contact_person_name = '';
    public string $contact_person_id = '';
    public string $email = '';
    public string $phone = '';
    public string $password = '';
    public string $preferred_language = 'ar';

    public function rules(): array
    {
        return [
            'company_name' => ['required', 'string', 'max:255'],
            'company_cert_number' => ['required', 'string', 'max:100', 'unique:users,company_cert_number'],
            'contact_person_name' => ['required', 'string', 'max:255'],
            'contact_person_id' => ['required', 'string', 'max:50'],
            'email' => ['required', 'email', 'max:255', 'unique:users,email'],
            'phone' => ['required', 'string', 'max:20'],
            'password' => ['required', 'string', 'min:8'],
            'preferred_language' => ['required', 'in:ar,en'],
        ];
    }

    public function messages(): array
    {
        return [
            'company_cert_number.unique' => __('clients.registration_number_exists'),
            'email.unique' => __('clients.email_exists'),
        ];
    }

    public function create(): void
    {
        $validated = $this->validate();

        $plainPassword = $validated['password'];

        $user = User::create([
            'user_type' => UserType::Company,
            'full_name' => $validated['company_name'],
            'company_name' => $validated['company_name'],
            'company_cert_number' => $validated['company_cert_number'],
            'contact_person_name' => $validated['contact_person_name'],
            'contact_person_id' => $validated['contact_person_id'],
            'email' => $validated['email'],
            'phone' => $validated['phone'],
            'password' => Hash::make($plainPassword),
            'preferred_language' => $validated['preferred_language'],
            'status' => UserStatus::Active,
        ]);

        AdminLog::create([
            'admin_id' => auth()->id(),
            'action' => 'create',
            'target_type' => 'user',
            'target_id' => $user->id,
            'new_values' => $user->only(['company_name', 'email', 'company_cert_number', 'contact_person_name', 'phone', 'preferred_language']),
            'ip_address' => request()->ip(),
            'created_at' => now(),
        ]);

        $user->notify(new WelcomeAccountNotification($plainPassword));

        session()->flash('success', __('clients.company_created'));

        $this->redirect(route('admin.clients.company.index'), navigate: true);
    }
}; ?>

<div>
    <div class="mb-6">
        <flux:button variant="ghost" :href="route('admin.clients.company.index')" wire:navigate icon="arrow-left">
            {{ __('clients.back_to_companies') }}
        </flux:button>
    </div>

    <div class="mb-6">
        <flux:heading size="xl">{{ __('clients.create_company') }}</flux:heading>
        <flux:text class="mt-1 text-zinc-500 dark:text-zinc-400">{{ __('clients.company_clients') }}</flux:text>
    </div>

    <div class="rounded-lg border border-zinc-200 bg-white p-6 dark:border-zinc-700 dark:bg-zinc-800">
        <form wire:submit="create" class="space-y-6">
            <div class="grid gap-6 sm:grid-cols-2">
                <flux:field>
                    <flux:label class="required">{{ __('clients.company_name') }}</flux:label>
                    <flux:input
                        wire:model="company_name"
                        type="text"
                        required
                        autofocus
                    />
                    <flux:error name="company_name" />
                </flux:field>

                <flux:field>
                    <flux:label class="required">{{ __('clients.registration_number') }}</flux:label>
                    <flux:input
                        wire:model="company_cert_number"
                        type="text"
                        required
                    />
                    <flux:error name="company_cert_number" />
                </flux:field>

                <flux:field>
                    <flux:label class="required">{{ __('clients.contact_person_name') }}</flux:label>
                    <flux:input
                        wire:model="contact_person_name"
                        type="text"
                        required
                    />
                    <flux:error name="contact_person_name" />
                </flux:field>

                <flux:field>
                    <flux:label class="required">{{ __('clients.contact_person_id') }}</flux:label>
                    <flux:input
                        wire:model="contact_person_id"
                        type="text"
                        required
                    />
                    <flux:error name="contact_person_id" />
                </flux:field>

                <flux:field>
                    <flux:label class="required">{{ __('clients.email') }}</flux:label>
                    <flux:input
                        wire:model="email"
                        type="email"
                        required
                    />
                    <flux:error name="email" />
                </flux:field>

                <flux:field>
                    <flux:label class="required">{{ __('clients.phone') }}</flux:label>
                    <flux:input
                        wire:model="phone"
                        type="tel"
                        required
                    />
                    <flux:error name="phone" />
                </flux:field>

                <flux:field>
                    <flux:label class="required">{{ __('clients.password') }}</flux:label>
                    <flux:input
                        wire:model="password"
                        type="password"
                        required
                    />
                    <flux:error name="password" />
                </flux:field>

                <flux:field>
                    <flux:label class="required">{{ __('clients.preferred_language') }}</flux:label>
                    <flux:select wire:model="preferred_language" required>
                        <flux:select.option value="ar">{{ __('clients.arabic') }}</flux:select.option>
                        <flux:select.option value="en">{{ __('clients.english') }}</flux:select.option>
                    </flux:select>
                    <flux:error name="preferred_language" />
                </flux:field>
            </div>

            <div class="flex items-center justify-end gap-4 border-t border-zinc-200 pt-6 dark:border-zinc-700">
                <flux:button variant="ghost" :href="route('admin.clients.company.index')" wire:navigate>
                    {{ __('clients.cancel') }}
                </flux:button>
                <flux:button variant="primary" type="submit">
                    {{ __('clients.create') }}
                </flux:button>
            </div>
        </form>
    </div>
</div>
