<?php

use App\Enums\UserStatus;
use App\Models\AdminLog;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rule;
use Livewire\Volt\Component;

new class extends Component {
    public User $client;

    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 = '';
    public string $status = '';

    public function mount(User $client): void
    {
        $this->client = $client;
        $this->company_name = $client->company_name ?? '';
        $this->company_cert_number = $client->company_cert_number ?? '';
        $this->contact_person_name = $client->contact_person_name ?? '';
        $this->contact_person_id = $client->contact_person_id ?? '';
        $this->email = $client->email;
        $this->phone = $client->phone ?? '';
        $this->preferred_language = $client->preferred_language ?? 'ar';
        $this->status = $client->status->value;
    }

    public function rules(): array
    {
        return [
            'company_name' => ['required', 'string', 'max:255'],
            'company_cert_number' => ['required', 'string', 'max:100', Rule::unique('users', 'company_cert_number')->ignore($this->client->id)],
            'contact_person_name' => ['required', 'string', 'max:255'],
            'contact_person_id' => ['required', 'string', 'max:50'],
            'email' => ['required', 'email', 'max:255', Rule::unique('users', 'email')->ignore($this->client->id)],
            'phone' => ['required', 'string', 'max:20'],
            'password' => ['nullable', 'string', 'min:8'],
            'preferred_language' => ['required', 'in:ar,en'],
            'status' => ['required', 'in:active,deactivated'],
        ];
    }

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

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

        $oldValues = $this->client->only(['company_name', 'email', 'company_cert_number', 'contact_person_name', 'contact_person_id', 'phone', 'preferred_language', 'status']);

        $this->client->company_name = $validated['company_name'];
        $this->client->full_name = $validated['company_name'];
        $this->client->company_cert_number = $validated['company_cert_number'];
        $this->client->contact_person_name = $validated['contact_person_name'];
        $this->client->contact_person_id = $validated['contact_person_id'];
        $this->client->email = $validated['email'];
        $this->client->phone = $validated['phone'];
        $this->client->preferred_language = $validated['preferred_language'];
        $this->client->status = $validated['status'];

        if (! empty($validated['password'])) {
            $this->client->password = Hash::make($validated['password']);
        }

        $this->client->save();

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

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

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

    public function with(): array
    {
        return [
            'statuses' => UserStatus::cases(),
        ];
    }
}; ?>

<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.edit_company') }}</flux:heading>
        <flux:text class="mt-1 text-zinc-500 dark:text-zinc-400">{{ $client->company_name }}</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="update" 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>{{ __('clients.password') }}</flux:label>
                    <flux:input
                        wire:model="password"
                        type="password"
                        placeholder="{{ __('Leave blank to keep current password') }}"
                    />
                    <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>

                <flux:field>
                    <flux:label class="required">{{ __('clients.status') }}</flux:label>
                    <flux:select wire:model="status" required>
                        @foreach ($statuses as $statusOption)
                            <flux:select.option value="{{ $statusOption->value }}">
                                {{ __('clients.' . $statusOption->value) }}
                            </flux:select.option>
                        @endforeach
                    </flux:select>
                    <flux:error name="status" />
                </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.update') }}
                </flux:button>
            </div>
        </form>
    </div>
</div>
