<?php

use App\Enums\UserType;
use App\Models\AdminLog;
use App\Models\User;
use App\Notifications\AccountTypeChangedNotification;
use Illuminate\Support\Facades\DB;
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 bool $showConfirmation = false;

    public function mount(User $client): void
    {
        $this->client = $client;
        $this->contact_person_name = $client->full_name;
        $this->contact_person_id = $client->national_id ?? '';
    }

    public function showConfirmationDialog(): void
    {
        $this->validate([
            'company_name' => 'required|string|max:255',
            'company_cert_number' => 'required|string|max:255|unique:users,company_cert_number',
            'contact_person_name' => 'required|string|max:255',
            'contact_person_id' => 'required|string|max:255',
        ]);

        $this->showConfirmation = true;
    }

    public function cancelConfirmation(): void
    {
        $this->showConfirmation = false;
    }

    public function convertToCompany(): void
    {
        $this->validate([
            'company_name' => 'required|string|max:255',
            'company_cert_number' => 'required|string|max:255|unique:users,company_cert_number',
            'contact_person_name' => 'required|string|max:255',
            'contact_person_id' => 'required|string|max:255',
        ]);

        $oldValues = $this->client->only([
            'user_type', 'full_name', 'national_id',
            'company_name', 'company_cert_number',
            'contact_person_name', 'contact_person_id',
        ]);

        DB::transaction(function () use ($oldValues) {
            $this->client->update([
                'user_type' => UserType::Company,
                'full_name' => $this->company_name,
                'company_name' => $this->company_name,
                'company_cert_number' => $this->company_cert_number,
                'contact_person_name' => $this->contact_person_name,
                'contact_person_id' => $this->contact_person_id,
                'national_id' => null,
            ]);

            AdminLog::create([
                'admin_id' => auth()->id(),
                'action' => 'convert_account',
                'target_type' => 'user',
                'target_id' => $this->client->id,
                'old_values' => $oldValues,
                'new_values' => $this->client->fresh()->only([
                    'user_type', 'full_name', 'national_id',
                    'company_name', 'company_cert_number',
                    'contact_person_name', 'contact_person_id',
                ]),
                'ip_address' => request()->ip(),
                'created_at' => now(),
            ]);

            try {
                $this->client->notify(new AccountTypeChangedNotification(UserType::Company));
            } catch (\Exception $e) {
                report($e);
            }
        });

        session()->flash('success', __('clients.account_converted_to_company'));
        $this->redirect(route('admin.clients.company.show', $this->client), navigate: true);
    }
}; ?>

<div>
    <flux:modal name="convert-to-company" class="md:w-96">
        <div class="space-y-6">
            <div>
                <flux:heading size="lg">{{ __('clients.convert_to_company') }}</flux:heading>
                <flux:text class="mt-2 text-zinc-600 dark:text-zinc-400">
                    {{ __('clients.convert_to_company_description') }}
                </flux:text>
            </div>

            @if (!$showConfirmation)
                <form wire:submit="showConfirmationDialog" class="space-y-4">
                    <flux:field>
                        <flux:label>{{ __('clients.company_name') }}</flux:label>
                        <flux:input wire:model="company_name" required />
                        <flux:error name="company_name" />
                    </flux:field>

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

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

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

                    <div class="flex gap-2 pt-4">
                        <flux:button type="button" variant="ghost" x-on:click="$flux.modal('convert-to-company').close()">
                            {{ __('clients.cancel') }}
                        </flux:button>
                        <flux:button type="submit" variant="primary">
                            {{ __('clients.continue') }}
                        </flux:button>
                    </div>
                </form>
            @else
                <div class="space-y-4">
                    <flux:callout variant="warning" icon="exclamation-triangle">
                        <flux:callout.heading>{{ __('clients.confirm_conversion') }}</flux:callout.heading>
                        <flux:callout.text>{{ __('clients.confirm_conversion_to_company_message') }}</flux:callout.text>
                    </flux:callout>

                    <div class="rounded-lg border border-zinc-200 p-4 dark:border-zinc-700">
                        <div class="space-y-2 text-sm">
                            <div class="flex justify-between">
                                <span class="text-zinc-500 dark:text-zinc-400">{{ __('clients.company_name') }}:</span>
                                <span class="font-medium">{{ $company_name }}</span>
                            </div>
                            <div class="flex justify-between">
                                <span class="text-zinc-500 dark:text-zinc-400">{{ __('clients.registration_number') }}:</span>
                                <span class="font-medium">{{ $company_cert_number }}</span>
                            </div>
                            <div class="flex justify-between">
                                <span class="text-zinc-500 dark:text-zinc-400">{{ __('clients.contact_person_name') }}:</span>
                                <span class="font-medium">{{ $contact_person_name }}</span>
                            </div>
                        </div>
                    </div>

                    <div class="flex gap-2 pt-4">
                        <flux:button type="button" variant="ghost" wire:click="cancelConfirmation">
                            {{ __('clients.back') }}
                        </flux:button>
                        <flux:button type="button" variant="primary" wire:click="convertToCompany">
                            {{ __('clients.confirm_convert') }}
                        </flux:button>
                    </div>
                </div>
            @endif
        </div>
    </flux:modal>
</div>
