<?php

namespace App\Livewire\Admin;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Password;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Title;
use Livewire\Component;

#[Layout('layouts.admin')]
#[Title('Profil Saya')]
class Profile extends Component
{
    // ── Tab aktif ────────────────────────────────────────────────────────
    public string $activeTab = 'profile'; // profile | password

    // ── Form profil ──────────────────────────────────────────────────────
    public string $name  = '';
    public string $phone = '';
    public string $email = '';

    // ── Form password ────────────────────────────────────────────────────
    public string $current_password  = '';
    public string $new_password      = '';
    public string $confirm_password  = '';

    public function mount(): void
    {
        $user        = Auth::user();
        $this->name  = $user->name;
        $this->phone = $user->phone ?? '';
        $this->email = $user->email ?? '';
    }

    // ── Simpan perubahan profil ───────────────────────────────────────────

    public function saveProfile(): void
    {
        $user = Auth::user();

        $validated = $this->validate([
            'name'  => ['required', 'string', 'max:100'],
            'phone' => ['nullable', 'string', 'max:20', Rule::unique('users', 'phone')->ignore($user->id)],
            'email' => ['nullable', 'email', 'max:100', Rule::unique('users', 'email')->ignore($user->id)],
        ], [
            'name.required'  => 'Nama wajib diisi.',
            'phone.unique'   => 'Nomor HP sudah digunakan akun lain.',
            'email.unique'   => 'Email sudah digunakan akun lain.',
        ]);

        $user->update([
            'name'  => $validated['name'],
            'phone' => $validated['phone'] ?: null,
            'email' => $validated['email'] ?: null,
        ]);

        activity('profile')
            ->causedBy($user)
            ->log('Memperbarui profil');

        $this->dispatch('notify', type: 'success', message: 'Profil berhasil diperbarui.');
    }

    // ── Ganti password ────────────────────────────────────────────────────

    public function savePassword(): void
    {
        $user = Auth::user();

        $this->validate([
            'current_password' => ['required', 'string'],
            'new_password'     => ['required', 'string', 'min:8', Password::defaults()],
            'confirm_password' => ['required', 'same:new_password'],
        ], [
            'current_password.required' => 'Password saat ini wajib diisi.',
            'new_password.required'     => 'Password baru wajib diisi.',
            'new_password.min'          => 'Password minimal 8 karakter.',
            'confirm_password.same'     => 'Konfirmasi password tidak cocok.',
        ]);

        if (! Hash::check($this->current_password, $user->password)) {
            $this->addError('current_password', 'Password saat ini tidak benar.');
            return;
        }

        $user->update([
            'password' => Hash::make($this->new_password),
        ]);

        activity('profile')
            ->causedBy($user)
            ->log('Mengganti password');

        $this->current_password = '';
        $this->new_password     = '';
        $this->confirm_password = '';

        $this->dispatch('notify', type: 'success', message: 'Password berhasil diubah.');
    }

    public function render()
    {
        return view('livewire.admin.profile', [
            'user' => Auth::user(),
        ]);
    }
}
