<?php

namespace App\Livewire\Admin;

use App\Models\Company;
use App\Models\Country;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Livewire\Component;

class UpdateUser extends Component
{
    public $id;
    public $first_name;
    public $last_name;
    public $email;
    public $company_type;
    public $password;

    public $company_id;
    public $company_name;
    public $address;
    public $address_2;
    public $post_code;
    public $country;
    public $description;

    protected $rules = [
        'first_name'    => 'required|string',
        'last_name'     => 'required|string',
        'company_type'  => 'required',

        'company_name'  => 'required|string',
        'address'       => 'required|string',
        'post_code'     => 'required|string',
        'country'       => 'required|string',
        'description'   => 'required|string',
    ];

    public function mount($id=null){
        $this->id = $id;
        if($this->id != null){
            $user = User::with('company')->find($id);

            $this->first_name = $user->first_name;
            $this->last_name = $user->last_name;
            $this->email = $user->email;
            $this->company_type = $user->company_type;

            if($user->company != null){
                $this->company_id = $user->company->id;
                $this->company_name = $user->company->name;
                $this->address = $user->company->address;
                $this->address_2 = $user->company->city;
                $this->post_code = $user->company->post_code;
                $this->country = $user->company->country;
                $this->description = $user->company->description;
            }
        }
    }

    public function render()
    {
        $countries = Country::pluck('name')->toArray();
        return view('livewire.admin.update-user', compact('countries'));
    }

    public function updateDetail(){
        $this->validate();
        $password = $this->password;
        $user = User::find($this->id);

        if($password != null){
            if (!preg_match('/[A-Z]/', $password)) {
                return $this->addError('password', 'The password must include at least one uppercase letter.');
            }
    
            if (!preg_match('/[a-z]/', $password)) {
                return $this->addError('password', 'The password must include at least one lowercase letter.');
            }
    
            if (!preg_match('/[0-9]/', $password)) {
                return $this->addError('password', 'The password must include at least one number.');
            }
    
            if (!preg_match('/[\W_]/', $password)) {
                return $this->addError('password', 'The password must include at least one special character.');
            }

            $user->password = Hash::make($password);
        }

        $user->first_name = $this->first_name;
        $user->last_name = $this->last_name;
        $user->company_type = $this->company_type;
        $user->save();

        Company::where('id', $this->company_id)->update([
            'address' => $this->address,
            'city' => $this->address_2,
            'post_code' => $this->post_code,
            'country' => $this->country,
            'type' => $this->company_type,
            'description' => $this->description,
        ]);

        $this->dispatch('detail_updated');
    }
}
