<?php

use App\Enums\PotentialClientType;
use App\Models\PotentialClient;
use Livewire\Volt\Component;

new class extends Component {
    public PotentialClient $potentialClient;

    public string $type = '';
    public string $name = '';
    public string $phone = '';
    public string $email = '';
    public string $address = '';
    public string $social_media = '';
    public string $website = '';
    public string $notes = '';

    public function mount(PotentialClient $potentialClient): void
    {
        $this->potentialClient = $potentialClient;
        $this->type = $potentialClient->type->value;
        $this->name = $potentialClient->name ?? '';
        $this->phone = $potentialClient->phone ?? '';
        $this->email = $potentialClient->email ?? '';
        $this->address = $potentialClient->address ?? '';
        $this->social_media = $potentialClient->social_media ?? '';
        $this->website = $potentialClient->website ?? '';
        $this->notes = $potentialClient->notes ?? '';
    }

    public function rules(): array
    {
        return [
            'type' => ['required', 'in:individual,company,agency'],
            'name' => ['nullable', 'string', 'max:255'],
            'phone' => ['nullable', 'string', 'max:50'],
            'email' => ['nullable', 'email', 'max:255'],
            'address' => ['nullable', 'string', 'max:1000'],
            'social_media' => ['nullable', 'string', 'max:255'],
            'website' => ['nullable', 'url', 'max:255'],
            'notes' => ['nullable', 'string', 'max:5000'],
        ];
    }

    public function messages(): array
    {
        return [
            'type.required' => __('potential-clients.type_required'),
        ];
    }

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

        $this->potentialClient->update($validated);

        session()->flash('success', __('potential-clients.updated_success'));
        $this->redirect(route('admin.potential-clients.show', $this->potentialClient), navigate: true);
    }

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

<div>
    <div class="mb-6">
        <flux:button variant="outline" :href="route('admin.potential-clients.show', $potentialClient)" wire:navigate icon="arrow-left">
            {{ __('potential-clients.back_to_details') }}
        </flux:button>
    </div>

    <div class="mb-6">
        <flux:heading size="xl">{{ __('potential-clients.edit_potential_client') }}</flux:heading>
        <flux:text class="mt-1 text-zinc-500">{{ $potentialClient->name ?? __('potential-clients.not_provided') }}</flux:text>
    </div>

    <div class="rounded-lg border border-zinc-200 bg-white p-6">
        <form wire:submit="update" class="space-y-6">
            {{-- Type Selection --}}
            <div>
                <flux:heading size="lg" class="mb-4">{{ __('potential-clients.fields.type') }}</flux:heading>
                <flux:field>
                    <flux:label class="required">{{ __('potential-clients.fields.type') }}</flux:label>
                    <flux:select wire:model="type" required>
                        <flux:select.option value="">{{ __('potential-clients.select_type') }}</flux:select.option>
                        @foreach ($types as $typeOption)
                            <flux:select.option value="{{ $typeOption->value }}">
                                {{ $typeOption->label() }}
                            </flux:select.option>
                        @endforeach
                    </flux:select>
                    <flux:error name="type" />
                </flux:field>
            </div>

            {{-- Contact Information --}}
            <div class="border-t border-zinc-200 pt-6">
                <flux:heading size="lg" class="mb-4">{{ __('potential-clients.contact_information') }}</flux:heading>
                <div class="grid gap-6 sm:grid-cols-2">
                    <flux:field>
                        <flux:label>{{ __('potential-clients.fields.name') }}</flux:label>
                        <flux:input
                            wire:model="name"
                            type="text"
                        />
                        <flux:error name="name" />
                    </flux:field>

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

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

                    <flux:field>
                        <flux:label>{{ __('potential-clients.fields.website') }}</flux:label>
                        <flux:input
                            wire:model="website"
                            type="url"
                            placeholder="https://"
                        />
                        <flux:error name="website" />
                    </flux:field>

                    <flux:field class="sm:col-span-2">
                        <flux:label>{{ __('potential-clients.fields.address') }}</flux:label>
                        <flux:textarea
                            wire:model="address"
                            rows="2"
                        />
                        <flux:error name="address" />
                    </flux:field>

                    <flux:field class="sm:col-span-2">
                        <flux:label>{{ __('potential-clients.fields.social_media') }}</flux:label>
                        <flux:input
                            wire:model="social_media"
                            type="text"
                            :placeholder="__('potential-clients.social_media_placeholder')"
                        />
                        <flux:error name="social_media" />
                    </flux:field>
                </div>
            </div>

            {{-- Additional Information --}}
            <div class="border-t border-zinc-200 pt-6">
                <flux:heading size="lg" class="mb-4">{{ __('potential-clients.additional_information') }}</flux:heading>
                <flux:field>
                    <flux:label>{{ __('potential-clients.fields.notes') }}</flux:label>
                    <flux:textarea
                        wire:model="notes"
                        rows="4"
                    />
                    <flux:error name="notes" />
                </flux:field>
            </div>

            <div class="flex items-center justify-end gap-4 border-t border-zinc-200 pt-6">
                <flux:button variant="outline" :href="route('admin.potential-clients.show', $potentialClient)" wire:navigate>
                    {{ __('potential-clients.cancel') }}
                </flux:button>
                <flux:button variant="primary" type="submit">
                    {{ __('potential-clients.save') }}
                </flux:button>
            </div>
        </form>
    </div>
</div>
