<?php

namespace App\Livewire\Merchant;

use App\Models\UserAddress;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Title;
use Livewire\Component;

#[Layout('layouts.merchant')]
#[Title('الملف الشخصي - تاجر بلس')]
class ProfilePage extends Component
{
    public string $shop_name = '';
    public string $governorate = '';
    public string $city = '';
    public string $address_details = '';
    public string $landmark = '';
    public string $extra_phone = '';
    public bool $is_default = false;

    public ?int $editingAddressId = null;
    public bool $showAddressForm = false;

    public function addNewAddress(): void
    {
        $this->resetAddressForm();
        $this->showAddressForm = true;
        $this->editingAddressId = null;
    }

    public function editAddress(int $id): void
    {
        $address = auth()->user()->addresses()->findOrFail($id);
        $this->editingAddressId = $id;
        $this->shop_name = $address->shop_name ?? '';
        $this->governorate = $address->governorate;
        $this->city = $address->city ?? '';
        $this->address_details = $address->address_details;
        $this->landmark = $address->landmark ?? '';
        $this->extra_phone = $address->extra_phone ?? '';
        $this->is_default = $address->is_default;
        $this->showAddressForm = true;
    }

    public function saveAddress(): void
    {
        $this->validate([
            'governorate' => 'required|string',
            'address_details' => 'required|string|max:500',
            'shop_name' => 'nullable|string|max:255',
            'city' => 'nullable|string|max:255',
            'landmark' => 'nullable|string|max:255',
            'extra_phone' => 'nullable|string|max:20',
        ]);

        $user = auth()->user();

        if ($this->is_default) {
            $user->addresses()->update(['is_default' => false]);
        }

        $data = [
            'shop_name' => $this->shop_name ?: null,
            'governorate' => $this->governorate,
            'city' => $this->city ?: null,
            'address_details' => $this->address_details,
            'landmark' => $this->landmark ?: null,
            'extra_phone' => $this->extra_phone ?: null,
            'is_default' => $this->is_default,
        ];

        if ($this->editingAddressId) {
            $user->addresses()->where('id', $this->editingAddressId)->update($data);
            session()->flash('success', 'تم تحديث العنوان بنجاح');
        } else {
            if ($user->addresses()->count() === 0) {
                $data['is_default'] = true;
            }
            $user->addresses()->create($data);
            session()->flash('success', 'تم إضافة العنوان بنجاح');
        }

        $this->resetAddressForm();
        $this->showAddressForm = false;
    }

    public function setDefault(int $id): void
    {
        $user = auth()->user();
        $user->addresses()->update(['is_default' => false]);
        $user->addresses()->where('id', $id)->update(['is_default' => true]);
    }

    public function deleteAddress(int $id): void
    {
        $address = auth()->user()->addresses()->findOrFail($id);
        $wasDefault = $address->is_default;
        $address->delete();

        if ($wasDefault) {
            $first = auth()->user()->addresses()->first();
            $first?->update(['is_default' => true]);
        }

        session()->flash('success', 'تم حذف العنوان');
    }

    public function cancelForm(): void
    {
        $this->resetAddressForm();
        $this->showAddressForm = false;
    }

    protected function resetAddressForm(): void
    {
        $this->shop_name = '';
        $this->governorate = '';
        $this->city = '';
        $this->address_details = '';
        $this->landmark = '';
        $this->extra_phone = '';
        $this->is_default = false;
        $this->editingAddressId = null;
    }

    public function render()
    {
        return view('livewire.merchant.profile-page', [
            'addresses' => auth()->user()->addresses()->orderByDesc('is_default')->get(),
            'governorates' => UserAddress::getEgyptGovernorates(),
        ]);
    }
}
