#!/bin/bash
# SCAR Chat - Quick Build and Test Script
# This script builds the project and sets up a test environment

set -e  # Exit on error

PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$PROJECT_ROOT"

echo "================================"
echo "SCAR Chat - Build & Test Setup"
echo "================================"
echo ""

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

# Check for dependencies
echo "Checking dependencies..."
MISSING_DEPS=()

command -v cmake >/dev/null 2>&1 || MISSING_DEPS+=("cmake")
command -v g++ >/dev/null 2>&1 || MISSING_DEPS+=("g++")
command -v sqlite3 >/dev/null 2>&1 || MISSING_DEPS+=("sqlite3")
command -v openssl >/dev/null 2>&1 || MISSING_DEPS+=("openssl")

if [ ${#MISSING_DEPS[@]} -ne 0 ]; then
    echo -e "${RED}Missing dependencies: ${MISSING_DEPS[*]}${NC}"
    echo ""
    echo "Install with:"
    echo "  Ubuntu/Debian: sudo apt install build-essential cmake qt6-base-dev libboost-all-dev libssl-dev libsqlite3-dev"
    echo "  Fedora: sudo dnf install gcc-c++ cmake qt6-qtbase-devel boost-devel openssl-devel sqlite-devel"
    exit 1
fi

echo -e "${GREEN}✓ Dependencies found${NC}"
echo ""

# Build
echo "Building project..."
BUILD_TYPE="${1:-Release}"

cmake -B build -DCMAKE_BUILD_TYPE="$BUILD_TYPE"
cmake --build build -j$(nproc)

echo -e "${GREEN}✓ Build complete${NC}"
echo ""

# Generate SSL certificates if not present
if [ ! -f "server.pem" ] || [ ! -f "server.key" ]; then
    echo "Generating self-signed SSL certificates..."
    openssl req -x509 -newkey rsa:4096 \
        -keyout server.key -out server.pem \
        -days 365 -nodes \
        -subj "/C=US/ST=State/L=City/O=SCAR/CN=localhost" \
        2>/dev/null
    echo -e "${GREEN}✓ SSL certificates generated${NC}"
else
    echo -e "${YELLOW}SSL certificates already exist, skipping...${NC}"
fi
echo ""

# Initialize database with test user
if [ ! -f "scarchat.db" ]; then
    echo "Creating test database with user..."
    
    # Create database and add test user using dbmanager
    ./build/dbmanager/dbmanager adduser testuser testpass
    
    if [ -f "scarchat.db" ]; then
        echo -e "${GREEN}✓ Database created with test user 'testuser'${NC}"
        echo "  Username: testuser"
        echo "  Password: testpass"
    else
        echo -e "${RED}Failed to create database${NC}"
    fi
else
    echo -e "${YELLOW}Database already exists, skipping...${NC}"
    echo ""
    echo "To add users manually:"
    echo "  ./build/dbmanager/dbmanager adduser <username> <password> [avatar]"
    echo ""
    echo "To list users:"
    echo "  ./build/dbmanager/dbmanager list"
fi

echo ""
echo "================================"
echo "Build Summary"
echo "================================"
echo ""
echo "Binaries:"
echo "  Server:    ./build/server/scarchat-server"
echo "  Client:    ./build/client/scarchat"
echo "  DBManager: ./build/dbmanager/dbmanager"
echo ""
echo "Configuration:"
echo "  Database: ./scarchat.db"
echo "  SSL Cert: ./server.pem"
echo "  SSL Key:  ./server.key"
echo ""
echo "To run:"
echo "  Terminal 1: ./build/server/scarchat-server"
echo "  Terminal 2: ./build/client/scarchat"
echo ""
echo "See QUICKSTART.md for detailed instructions"
echo ""
