<?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 $full_name = '';
    public string $national_id = '';

    public bool $showConfirmation = false;

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

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

        $this->showConfirmation = true;
    }

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

    public function convertToIndividual(): void
    {
        $this->validate([
            'full_name' => 'required|string|max:255',
            'national_id' => 'required|string|max:255|unique:users,national_id',
        ]);

        $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::Individual,
                'full_name' => $this->full_name,
                'national_id' => $this->national_id,
                'company_name' => null,
                'company_cert_number' => null,
                'contact_person_name' => null,
                'contact_person_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::Individual));
            } catch (\Exception $e) {
                report($e);
            }
        });

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

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

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

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

                    <flux:callout variant="info" icon="information-circle">
                        <flux:callout.text>{{ __('clients.company_fields_will_be_cleared') }}</flux:callout.text>
                    </flux:callout>

                    <div class="flex gap-2 pt-4">
                        <flux:button type="button" variant="outline" x-on:click="$flux.modal('convert-to-individual').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_individual_message') }}</flux:callout.text>
                    </flux:callout>

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

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