<?php

include_once 'config.php';
include_once 'helpers.php';

class Validation {

    public static function validate_post(array $request) {
        
        $validationErrors = [];
        $validationRules = Config::POST_PARAMS_REQ;
        $requiredRule = 'required';
        $inRule = 'in';
        $dateFormatRule = 'date_format';

        foreach ($validationRules as $field => $rules) {
            $rules = explode('|', $rules);

            foreach ($rules as $rule) {
                $elements = explode(':', $rule, 2);
                $rule = $elements[0];
                $params = isset($elements[1]) ? $elements[1] : null;

                if (! empty($rule)) {
                    switch ($rule) {
                        case $requiredRule:
                            if (empty($request[$field])) {
                                $validationErrors[$field][] = "The '$field' field is required.";
                            }
                            break;

                        case $inRule:
                            $params = $params ? explode(',', $params) : null;

                            if (! in_array($request[$field], $params)) {
                                $validationErrors[$field][] = "The '$field' must be in " . implode(', ', $params) . ".";
                            }
                            break;

                        case $dateFormatRule;
                            if (! empty($request[$field])) {
                                $date = \DateTime::createFromFormat($params, $request[$field]);

                                if (! $date || $date->format($params) !== $request[$field]) {
                                    $validationErrors[$field][] = "The '$field' must be in date format: {$params}.";
                                }
                            }
                            break;
                    }
                }
            }
        }

        $api_key_error = self::validate_api_key($request['api_key']);
        if (! empty($api_key_error)) {
            $validationErrors['api_key'][] = $api_key_error;
        }
        
        if (! empty($validationErrors)) {
            return $validationErrors;
        }
        return false;
    }



    public static function validate_image($file) {

        $file_extensions = Config::IMAGE_EXTENSIONS;
        $err_msg = [];
        $imageFileType = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
        if(!in_array($imageFileType, $file_extensions)) {
            $err_msg[] = $file["name"] . " is not allowed, only " . implode(", ", $file_extensions) . " files are allowed.";
        }
  
        // this must be used on actual fileupload
        // $check = getimagesize($file["tmp_name"]);
        // print_r($file);
        // if (empty($check)) {
        //     $err_msg[] = $file["tmp_name"] . " is not an image.";
        // }

        if ($file["size"] > Config::IMAGE_UPLOAD_SIZE) {
            $err_msg[] = $file["name"] . " is too large.";
        }

        return $err_msg;
    }


    public static function validate_api_key(string $key = null) {

        if ($key === null || !Helpers::verifyBWApiKey($key)) {
            return "API key do not match or missing";
        }
        return false;
    }
}


?>