<?php

declare(strict_types=1);

namespace App\Livewire\Admin\Users;

use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules\Password;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Rule;
use Livewire\Component;

#[Layout('layouts.admin')]
final class Edit extends Component
{
    public User $user;

    // User Details
    #[Rule('required|string|max:255')]
    public string $name = '';

    #[Rule('required|email|max:255|unique:users,email')]
    public string $email = '';

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

    #[Rule('nullable|string|max:15')]
    public ?string $phone = null;

    #[Rule('nullable|string|max:500')]
    public ?string $bio = null;

    // Password Change
    #[Rule('nullable|string|min:8')]
    public string $password = '';

    #[Rule('required_with:password|same:password')]
    public string $password_confirmation = '';

    public bool $showPasswordForm = false;

    public function mount(User $user): void
    {
        $this->user = $user;
        $this->name = $user->name;
        $this->email = $user->email;
        $this->status = $user->status;
        $this->phone = $user->phone;
        $this->bio = $user->bio;
    }

    public function updatedEmail(): void
    {
        $this->validateOnly('email', [
            'email' => 'required|email|max:255|unique:users,email,'.$this->user->id,
        ]);
    }

    public function togglePasswordForm(): void
    {
        $this->showPasswordForm = ! $this->showPasswordForm;
        $this->password = '';
        $this->password_confirmation = '';
    }

    public function updateUser(): void
    {
        $this->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|email|max:255|unique:users,email,'.$this->user->id,
            'status' => 'required|string|in:active,inactive,suspended',
            'phone' => 'nullable|string|max:15',
            'bio' => 'nullable|string|max:500',
        ]);

        $this->user->update([
            'name' => $this->name,
            'email' => $this->email,
            'status' => $this->status,
            'phone' => $this->phone,
            'bio' => $this->bio,
        ]);

        activity()
            ->causedBy(auth()->user())
            ->performedOn($this->user)
            ->withProperties([
                'user_id' => $this->user->id,
                'changes' => [
                    'name' => $this->name,
                    'email' => $this->email,
                    'status' => $this->status,
                    'phone' => $this->phone,
                    'bio' => $this->bio,
                ],
            ])
            ->log('user_updated');

        session()->flash('success', 'User details updated successfully!');
    }

    public function updatePassword(): void
    {
        $this->validate([
            'password' => ['required', 'confirmed', Password::min(8)
                ->letters()
                ->mixedCase()
                ->numbers()],
        ]);

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

        activity()
            ->causedBy(auth()->user())
            ->performedOn($this->user)
            ->withProperties([
                'user_id' => $this->user->id,
                'action' => 'password_changed',
            ])
            ->log('user_password_changed');

        $this->showPasswordForm = false;
        $this->password = '';
        $this->password_confirmation = '';

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

    public function toggleUserStatus(): void
    {
        $newStatus = $this->user->status === 'active' ? 'inactive' : 'active';

        $this->user->update(['status' => $newStatus]);
        $this->status = $newStatus;

        activity()
            ->causedBy(auth()->user())
            ->performedOn($this->user)
            ->withProperties([
                'user_id' => $this->user->id,
                'old_status' => $this->user->status === 'active' ? 'inactive' : 'active',
                'new_status' => $newStatus,
            ])
            ->log('user_status_toggled');

        session()->flash('success', "User status changed to {$newStatus}!");
    }

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