#!/bin/bash
# Norda Biznes - Quick View Scripts for Digital Maturity Results
#
# SECURITY NOTE: This script requires PGPASSWORD environment variable to be set
# Usage:
#   export PGPASSWORD='your_database_password'
#   ./view_maturity_results.sh
#
# NEVER hardcode passwords in scripts (CWE-798: Use of Hard-coded Credentials)

# Check if PGPASSWORD is set
if [ -z "$PGPASSWORD" ]; then
    echo "ERROR: PGPASSWORD environment variable is not set"
    echo "Please set it before running this script:"
    echo "  export PGPASSWORD='your_database_password'"
    echo "  ./view_maturity_results.sh"
    exit 1
fi

echo "====================================="
echo "NORDA BIZNES - DIGITAL MATURITY"
echo "====================================="
echo ""

# 1. Top 10 firms
echo "🏆 TOP 10 NAJLEPSZYCH FIRM:"
ssh root@10.22.68.249 "PGPASSWORD=\"$PGPASSWORD\" psql -h localhost -U nordabiz_app -d nordabiz -c \"
SELECT
    c.name,
    cdm.overall_score,
    cdm.online_presence_score,
    c.website
FROM companies c
JOIN company_digital_maturity cdm ON c.id = cdm.company_id
WHERE cdm.overall_score > 0
ORDER BY cdm.overall_score DESC
LIMIT 10;
\""

echo ""
echo "💰 TOP 10 SZANS SPRZEDAŻOWYCH:"
ssh root@10.22.68.249 "PGPASSWORD=\"$PGPASSWORD\" psql -h localhost -U nordabiz_app -d nordabiz -c \"
SELECT
    c.name,
    cdm.sales_readiness,
    cdm.total_opportunity_value,
    cwa.opportunity_score,
    array_to_string(cdm.critical_gaps, ', ') as gaps
FROM companies c
JOIN company_digital_maturity cdm ON c.id = cdm.company_id
JOIN company_website_analysis cwa ON c.id = cwa.company_id
WHERE cdm.total_opportunity_value > 0
ORDER BY cdm.total_opportunity_value DESC, cwa.opportunity_score DESC
LIMIT 10;
\""

echo ""
echo "📊 STATYSTYKI OGÓLNE:"
ssh root@10.22.68.249 "PGPASSWORD=\"$PGPASSWORD\" psql -h localhost -U nordabiz_app -d nordabiz -c \"
SELECT
    COUNT(*) as total_analyzed,
    ROUND(AVG(overall_score), 1) as avg_score,
    MAX(overall_score) as max_score,
    MIN(overall_score) as min_score,
    SUM(total_opportunity_value) as total_opportunity,
    COUNT(CASE WHEN sales_readiness = 'warm' THEN 1 END) as warm_leads,
    COUNT(CASE WHEN sales_readiness = 'cold' THEN 1 END) as cold_leads,
    COUNT(CASE WHEN sales_readiness = 'not_ready' THEN 1 END) as not_ready
FROM company_digital_maturity
WHERE overall_score > 0;
\""
