#!/usr/bin/env bash

DOMAIN_NAME="https://outils.chatons.duckdns.org"
DIAGRAM_DOMAIN_NAME="https://diagrams.chatons.duckdns.org"

GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m'

now=$(date +"%Y-%m-%d %H:%M:%S")

test_endpoint() {
    local name="$1"
    local url="$2"
    local expected_code="$3"
    local expected_body="$4"
    local match_mode="${5:-exact}"
    local follow_redirects="${6:-false}"

    echo -n "Testing $name... "

    local tmp
    tmp=$(mktemp)

    local curl_args=(-s -k -o "$tmp" -w "%{http_code}" "$url")
    if [ "$follow_redirects" = "true" ]; then
        curl_args=(-s -k -L -o "$tmp" -w "%{http_code}" "$url")
    fi

    local code
    code=$(curl "${curl_args[@]}" 2>/dev/null || echo "000")

    local body
    body=$(cat "$tmp")
    rm -f "$tmp"

    if [ "$code" != "$expected_code" ]; then
        echo -e "${RED}✗ FAIL${NC} (HTTP $code, expected $expected_code)"
        echo "Response: $body"
        return 1
    fi

    if [ -n "$expected_body" ]; then
        if [ "$match_mode" = "regex" ]; then
            if [[ ! "$body" =~ $expected_body ]]; then
                echo -e "${RED}✗ FAIL${NC} (body mismatch)"
                echo "Response: $body"
                return 1
            fi
        else
            if [ "$body" != "$expected_body" ]; then
                echo -e "${RED}✗ FAIL${NC} (body mismatch)"
                echo "Response: $body"
                return 1
            fi
        fi
    fi

    echo -e "${GREEN}✓ OK${NC} (HTTP $code)"
    return 0
}

test_endpoint_auth() {
    local name="$1"
    local url="$2"
    local username="$3"
    local password="$4"
    local expected_code="$5"
    local expected_body="$6"
    local match_mode="${7:-exact}"

    echo -n "Testing $name... "

    local tmp
    tmp=$(mktemp)

    local curl_args=(-s -k -o "$tmp" -w "%{http_code}" -u "$username:$password" "$url")

    local code
    code=$(curl "${curl_args[@]}" 2>/dev/null || echo "000")

    local body
    body=$(cat "$tmp")
    rm -f "$tmp"

    if [ "$code" != "$expected_code" ]; then
        echo -e "${RED}✗ FAIL${NC} (HTTP $code, expected $expected_code)"
        echo "Response: $body"
        return 1
    fi

    if [ -n "$expected_body" ]; then
        if [ "$match_mode" = "regex" ]; then
            if [[ ! "$body" =~ $expected_body ]]; then
                echo -e "${RED}✗ FAIL${NC} (body mismatch)"
                echo "Response: $body"
                return 1
            fi
        else
            if [ "$body" != "$expected_body" ]; then
                echo -e "${RED}✗ FAIL${NC} (body mismatch)"
                echo "Response: $body"
                return 1
            fi
        fi
    fi

    echo -e "${GREEN}✓ OK${NC} (HTTP $code)"
    return 0
}

test_endpoint "Health Check" "$DOMAIN_NAME/healthcheck" "200" "OK"
test_endpoint "Date Check" "$DOMAIN_NAME/date" "200" "^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$" "regex"
test_endpoint "IP check" "$DOMAIN_NAME/ip" "200" "^[0-9]{1,3}(\.[0-9]{1,3}){3}$" "regex"
test_endpoint "Encode base64" "$DOMAIN_NAME/b64/encode/test" "200" "dGVzdA=="
test_endpoint "Decode base64" "$DOMAIN_NAME/b64/decode/dGVzdA==" "200" "test"
test_endpoint "Hello Check" "$DOMAIN_NAME/hello/Chatons" "200" "Hello Chatons!"
test_endpoint "Teapot Check" "$DOMAIN_NAME/teapot" "418" "HTML Tea! Tea! Teapot!"
test_endpoint "Auth Check Unauthorized" "$DOMAIN_NAME/secretpage" "401" ""
test_endpoint_auth "Auth Check Authorized" "$DOMAIN_NAME/secretpage" "towel" "poisson" "200" "Welcome, towel" "exact"
test_endpoint_auth "Auth Check wrong password" "$DOMAIN_NAME/secretpage" "towel" "fakepassword" "401"
test_endpoint "Root redirect" "$DOMAIN_NAME/" "200" "Hello world!" "exact" "true"

