<?php
declare(strict_types=1);

require_once __DIR__ . '/Response.php';

class Validator
{
    /* =========================
     * REQUIRED FIELD CHECK
     * ========================= */
    public static function required(array $data, array $fields): void
    {
        foreach ($fields as $field) {

            if (!array_key_exists($field, $data)) {
                Response::error("$field is required", 422);
            }

            $value = $data[$field];

            if (is_array($value)) {
                if (count($value) === 0) {
                    Response::error("$field cannot be empty", 422);
                }
                continue;
            }

            if (is_string($value)) {
                if (trim($value) === '') {
                    Response::error("$field cannot be empty", 422);
                }
                continue;
            }

            if ($value === null) {
                Response::error("$field cannot be null", 422);
            }
        }
    }

    /* =========================
     * EMAIL VALIDATION
     * ========================= */
    public static function email(string $email): void
    {
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            Response::error('Invalid email address', 422);
        }
    }

    /* =========================
     * STRING LENGTH VALIDATION (NEW)
     * ========================= */
    public static function string(string $value, int $min, int $max): void
    {
        $length = mb_strlen(trim($value));

        if ($length < $min || $length > $max) {
            Response::error(
                "Value must be between {$min} and {$max} characters",
                422
            );
        }
    }

    /* =========================
     * MIN LENGTH VALIDATION
     * ========================= */
    public static function minLength(string $value, int $length, string $fieldName): void
    {
        if (mb_strlen(trim($value)) < $length) {
            Response::error(
                "$fieldName must be at least $length characters long",
                422
            );
        }
    }

    /* =========================
     * MAX LENGTH VALIDATION
     * ========================= */
    public static function maxLength(string $value, int $length, string $fieldName): void
    {
        if (mb_strlen(trim($value)) > $length) {
            Response::error(
                "$fieldName must not exceed $length characters",
                422
            );
        }
    }

    /* =========================
     * INTEGER VALIDATION
     * ========================= */
    public static function int($value, string $fieldName): void
    {
        if (!filter_var($value, FILTER_VALIDATE_INT)) {
            Response::error("$fieldName must be an integer", 422);
        }
    }

    /* =========================
     * ENUM VALIDATION
     * ========================= */
    public static function enum(string $value, array $allowed, string $fieldName): void
    {
        if (!in_array($value, $allowed, true)) {
            Response::error("$fieldName has invalid value", 422);
        }
    }

    /* =========================
     * PASSWORD STRENGTH (OPTIONAL)
     * ========================= */
    public static function strongPassword(string $password): void
    {
        if (
            strlen($password) < 8 ||
            !preg_match('/[A-Z]/', $password) ||
            !preg_match('/[a-z]/', $password) ||
            !preg_match('/[0-9]/', $password)
        ) {
            Response::error(
                'Password must contain uppercase, lowercase, number and be at least 8 characters',
                422
            );
        }
    }
}
