<?php

namespace App\Livewire\Customers;

use Livewire\Component;
use App\Models\Customer;
use Livewire\Attributes\Validate;

class Edit extends Component
{
    public Customer $customer;

    #[Validate('required|string|max:255')]
    public $first_name = '';

    #[Validate('required|string|max:255')]
    public $last_name = '';

    #[Validate('required|email|max:255|unique:customers,email')]
    public $email = '';

    #[Validate('required|string|max:20')]
    public $phone = '';

    #[Validate('nullable|string|max:20')]
    public $secondary_phone = '';

    #[Validate('required|string|max:500')]
    public $address = '';

    #[Validate('required|string|max:255')]
    public $city = '';

    #[Validate('required|string|max:255')]
    public $state = '';

    #[Validate('required|string|max:10')]
    public $zip_code = '';

    #[Validate('nullable|string|max:1000')]
    public $notes = '';

    #[Validate('required|in:active,inactive')]
    public $status = 'active';

    public function mount(Customer $customer)
    {
        $this->customer = $customer;
        $this->first_name = $customer->first_name;
        $this->last_name = $customer->last_name;
        $this->email = $customer->email;
        $this->phone = $customer->phone;
        $this->secondary_phone = $customer->secondary_phone;
        $this->address = $customer->address;
        $this->city = $customer->city;
        $this->state = $customer->state;
        $this->zip_code = $customer->zip_code;
        $this->notes = $customer->notes;
        $this->status = $customer->status;
    }

    public function updateCustomer()
    {
        // Update validation rules to exclude current customer's email
        $this->validate([
            'first_name' => 'required|string|max:255',
            'last_name' => 'required|string|max:255',
            'email' => 'required|email|max:255|unique:customers,email,' . $this->customer->id,
            'phone' => 'required|string|max:20',
            'secondary_phone' => 'nullable|string|max:20',
            'address' => 'required|string|max:500',
            'city' => 'required|string|max:255',
            'state' => 'required|string|max:255',
            'zip_code' => 'required|string|max:10',
            'notes' => 'nullable|string|max:1000',
            'status' => 'required|in:active,inactive',
        ]);

        $this->customer->update([
            'first_name' => $this->first_name,
            'last_name' => $this->last_name,
            'email' => $this->email,
            'phone' => $this->phone,
            'secondary_phone' => $this->secondary_phone,
            'address' => $this->address,
            'city' => $this->city,
            'state' => $this->state,
            'zip_code' => $this->zip_code,
            'notes' => $this->notes,
            'status' => $this->status,
        ]);

        session()->flash('success', 'Customer updated successfully!');

        return $this->redirect('/customers/' . $this->customer->id, navigate: true);
    }

    public function render()
    {
        return view('livewire.customers.edit');
    }
}
