<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

/**
 * Strong Password Validation Rule
 * Enforces strong password requirements for enhanced security
 */
class StrongPassword implements Rule
{
    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     */
    public function passes($attribute, $value): bool
    {
        if (! is_string($value) || strlen($value) < 8) {
            return false;
        }

        // Check for at least one uppercase letter
        if (! preg_match('/[A-Z]/', $value)) {
            return false;
        }

        // Check for at least one lowercase letter
        if (! preg_match('/[a-z]/', $value)) {
            return false;
        }

        // Check for at least one number
        if (! preg_match('/[0-9]/', $value)) {
            return false;
        }

        // Check for at least one special character
        if (! preg_match('/[^A-Za-z0-9]/', $value)) {
            return false;
        }

        return true;
    }

    /**
     * Get the validation error message.
     */
    public function message(): string
    {
        return 'The :attribute must be at least 8 characters and contain at least one uppercase letter, one lowercase letter, one number, and one special character.';
    }
}
