# KOT Social Login - Test Environment
# WordPress + WooCommerce + Playwright on k3s

namespace := "social-login-test"
release := "social-login-test"
image := "localhost/social-login-test:latest"
chart := "chart"

# Default recipe
default:
    @just --list

# Build the test image directly into k3s containerd
build-image:
    sudo nerdctl --address /run/k3s/containerd/containerd.sock --namespace k8s.io build -t {{image}} .
    @echo "Image built into k3s"

# Deploy test environment (build image + helm install)
deploy: build-image
    sudo mkdir -p /var/cache/social-login-test/mariadb
    helm upgrade --install {{release}} {{chart}} \
        --namespace {{namespace}} \
        --values {{chart}}/values.yaml \
        --create-namespace \
        --wait \
        --timeout 5m
    @echo ""
    @just status
    @echo ""
    @just info

# Wait for WordPress setup to finish (run after deploy)
wait-ready:
    #!/usr/bin/env bash
    set -euo pipefail
    echo "Waiting for WordPress setup..."
    POD=$(kubectl get pod -n {{namespace}} -l app={{release}} -o jsonpath='{.items[0].metadata.name}')
    for i in $(seq 1 60); do
        if kubectl exec -n {{namespace}} "$POD" -c wordpress -- test -f /var/www/html/.setup-done 2>/dev/null; then
            echo "WordPress is ready."
            exit 0
        fi
        sleep 3
    done
    echo "Timed out waiting for WordPress setup."
    exit 1

# Run Playwright tests (inside the pod)
test *ARGS: wait-ready
    #!/usr/bin/env bash
    set -euo pipefail
    POD=$(kubectl get pod -n {{namespace}} -l app={{release}} -o jsonpath='{.items[0].metadata.name}')
    kubectl exec -n {{namespace}} "$POD" -c wordpress -- \
        bash -c 'cd /tests && BASE_URL=http://social-login-test.k3s npx --prefix /opt/playwright playwright test {{ARGS}}'

# Run a specific test file
test-file FILE: wait-ready
    #!/usr/bin/env bash
    set -euo pipefail
    POD=$(kubectl get pod -n {{namespace}} -l app={{release}} -o jsonpath='{.items[0].metadata.name}')
    kubectl exec -n {{namespace}} "$POD" -c wordpress -- \
        bash -c 'cd /tests && BASE_URL=http://social-login-test.k3s npx --prefix /opt/playwright playwright test {{FILE}}'

# Show Playwright HTML report (copies from pod, opens locally)
test-report:
    #!/usr/bin/env bash
    set -euo pipefail
    POD=$(kubectl get pod -n {{namespace}} -l app={{release}} -o jsonpath='{.items[0].metadata.name}')
    mkdir -p /tmp/playwright-report
    kubectl cp -n {{namespace}} "$POD":/tests/playwright-report /tmp/playwright-report -c wordpress 2>/dev/null || true
    echo "Report at: /tmp/playwright-report/index.html"

# Exec into the WordPress container
shell:
    #!/usr/bin/env bash
    POD=$(kubectl get pod -n {{namespace}} -l app={{release}} -o jsonpath='{.items[0].metadata.name}')
    kubectl exec -it -n {{namespace}} "$POD" -c wordpress -- bash

# Run WP-CLI command inside the pod
wp *ARGS:
    #!/usr/bin/env bash
    POD=$(kubectl get pod -n {{namespace}} -l app={{release}} -o jsonpath='{.items[0].metadata.name}')
    kubectl exec -n {{namespace}} "$POD" -c wordpress -- wp {{ARGS}} --allow-root

# Check status
status:
    @echo "=== Pods ==="
    @kubectl get pods -n {{namespace}} -o wide
    @echo ""
    @echo "=== Containers ==="
    @kubectl get pods -n {{namespace}} -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{range .status.containerStatuses[*]}  {.name}: {.state}{"\n"}{end}{end}' 2>/dev/null || kubectl get pods -n {{namespace}}

# Watch pods
watch:
    kubectl get pods -n {{namespace}} -w

# Show WordPress logs
logs:
    kubectl logs -n {{namespace}} -l app={{release}} -c wordpress -f

# Show MariaDB logs
logs-db:
    kubectl logs -n {{namespace}} -l app={{release}} -c mariadb -f

# Show recent logs from both containers
logs-recent:
    @echo "=== WordPress (last 30 lines) ==="
    @kubectl logs -n {{namespace}} -l app={{release}} -c wordpress --tail=30
    @echo ""
    @echo "=== MariaDB (last 20 lines) ==="
    @kubectl logs -n {{namespace}} -l app={{release}} -c mariadb --tail=20

# Uninstall everything
uninstall:
    helm uninstall {{release}} -n {{namespace}} || true
    kubectl delete namespace {{namespace}} || true
    @echo "To also remove DB data: sudo rm -rf /var/cache/social-login-test"

# Reset database (delete MariaDB data, redeploy)
reset:
    just uninstall
    sudo rm -rf /var/cache/social-login-test/mariadb
    just deploy

# Template the chart (dry-run)
template:
    helm template {{release}} {{chart}} --values {{chart}}/values.yaml

# Lint the chart
lint:
    helm lint {{chart}}

# Show release history
history:
    @helm history {{release}} -n {{namespace}} --max 5

# Port-forward WordPress to localhost:8080
forward:
    @echo "WordPress at http://localhost:8080"
    kubectl port-forward -n {{namespace}} svc/{{release}} 8080:80

# Show connection info
info:
    @echo "=== Social Login Test Environment ==="
    @echo ""
    @echo "WordPress:  http://social-login-test.k3s"
    @echo "WP Admin:   http://social-login-test.k3s/wp-admin/"
    @echo "My Account: http://social-login-test.k3s/my-account/"
    @echo ""
    @echo "Admin:    admin / admin"
    @echo "Customer: customer / customer"
    @echo ""
    @echo "Commands:"
    @echo "  just browse            - switch siteurl for browser access"
    @echo "  just test              - run Playwright tests"
    @echo "  just test-file <file>  - run specific test"
    @echo "  just shell             - exec into container"
    @echo "  just wp <args>         - run WP-CLI"
    @echo "  just logs              - WordPress logs"
    @echo "  just reset             - wipe DB and redeploy"
