<?php
/**
 * SISPENRA - Validasi Input
 */

class Validator {
    private $errors = [];
    private $data = [];

    public function __construct($data) {
        $this->data = $data;
    }

    public function required($field, $label = null) {
        $label = $label ?: ucfirst(str_replace('_', ' ', $field));
        if (empty($this->data[$field])) {
            $this->errors[$field] = "{$label} wajib diisi.";
        }
        return $this;
    }

    public function minLength($field, $min, $label = null) {
        $label = $label ?: ucfirst(str_replace('_', ' ', $field));
        if (isset($this->data[$field]) && strlen($this->data[$field]) < $min) {
            $this->errors[$field] = "{$label} minimal {$min} karakter.";
        }
        return $this;
    }

    public function maxLength($field, $max, $label = null) {
        $label = $label ?: ucfirst(str_replace('_', ' ', $field));
        if (isset($this->data[$field]) && strlen($this->data[$field]) > $max) {
            $this->errors[$field] = "{$label} maksimal {$max} karakter.";
        }
        return $this;
    }

    public function email($field, $label = null) {
        $label = $label ?: ucfirst(str_replace('_', ' ', $field));
        if (!empty($this->data[$field]) && !filter_var($this->data[$field], FILTER_VALIDATE_EMAIL)) {
            $this->errors[$field] = "{$label} harus berupa email yang valid.";
        }
        return $this;
    }

    public function numeric($field, $label = null) {
        $label = $label ?: ucfirst(str_replace('_', ' ', $field));
        if (!empty($this->data[$field]) && !is_numeric($this->data[$field])) {
            $this->errors[$field] = "{$label} harus berupa angka.";
        }
        return $this;
    }

    public function between($field, $min, $max, $label = null) {
        $label = $label ?: ucfirst(str_replace('_', ' ', $field));
        if (!empty($this->data[$field])) {
            $val = floatval($this->data[$field]);
            if ($val < $min || $val > $max) {
                $this->errors[$field] = "{$label} harus antara {$min} dan {$max}.";
            }
        }
        return $this;
    }

    public function unique($field, $table, $column, $excludeId = null, $label = null) {
        global $pdo;
        $label = $label ?: ucfirst(str_replace('_', ' ', $field));
        if (!empty($this->data[$field])) {
            $sql = "SELECT id FROM {$table} WHERE {$column} = ?";
            $params = [$this->data[$field]];
            if ($excludeId) {
                $sql .= " AND id != ?";
                $params[] = $excludeId;
            }
            $stmt = $pdo->prepare($sql);
            $stmt->execute($params);
            if ($stmt->fetch()) {
                $this->errors[$field] = "{$label} sudah digunakan.";
            }
        }
        return $this;
    }

    public function fails() {
        return !empty($this->errors);
    }

    public function passes() {
        return empty($this->errors);
    }

    public function getErrors() {
        return $this->errors;
    }

    public function getFirstError() {
        return !empty($this->errors) ? array_values($this->errors)[0] : null;
    }
}

function validate($data) {
    return new Validator($data);
}

function validateNilai($nilai) {
    $errors = [];
    $fields = ['nilai_tugas', 'nilai_harian', 'nilai_uts', 'nilai_uas'];
    foreach ($fields as $field) {
        if (isset($nilai[$field]) && $nilai[$field] !== '') {
            $val = floatval($nilai[$field]);
            if ($val < 0 || $val > 100) {
                $errors[$field] = ucfirst(str_replace('_', ' ', $field)) . " harus antara 0-100.";
            }
        }
    }
    return $errors;
}
