#!/bin/bash

set -e

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

echo -e "${BLUE}🧪 Testing APT repository configuration...${NC}"

# Check if we have a Debian package
if [ ! -f "sesterce-cli_*_all.deb" ]; then
    echo -e "${YELLOW}No Debian package found, creating test structure...${NC}"
    
    # Create test repository structure
    mkdir -p test-apt-repo/conf
    mkdir -p test-apt-repo/db
    mkdir -p test-apt-repo/incoming
    mkdir -p test-apt-repo/pool
    
    # Create distributions file with common architectures (supports all packages)
    cat > test-apt-repo/conf/distributions << EOF
Origin: Sesterce CLI
Label: Sesterce CLI Repository
Codename: stable
Architectures: amd64 arm64 i386
Components: main
Description: Sesterce CLI Debian Package Repository
SignWith: default
EOF
    
    echo -e "${GREEN}✅ Test APT repository structure created${NC}"
    echo -e "${YELLOW}📋 Distributions file:${NC}"
    cat test-apt-repo/conf/distributions
    
    # Test reprepro configuration
    echo -e "${YELLOW}🧪 Testing reprepro configuration...${NC}"
    if reprepro -b test-apt-repo checkpool stable; then
        echo -e "${GREEN}✅ reprepro configuration is valid${NC}"
    else
        echo -e "${RED}❌ reprepro configuration has issues${NC}"
    fi
    
    # Clean up
    rm -rf test-apt-repo
else
    echo -e "${YELLOW}Found Debian package, testing with actual package...${NC}"
    
    # Get package filename
    PACKAGE_FILE=$(ls sesterce-cli_*_all.deb | head -1)
    echo -e "${BLUE}Package: ${PACKAGE_FILE}${NC}"
    
    # Create test repository structure
    mkdir -p test-apt-repo/conf
    mkdir -p test-apt-repo/db
    mkdir -p test-apt-repo/incoming
    mkdir -p test-apt-repo/pool
    
    # Create distributions file with common architectures (supports all packages)
    cat > test-apt-repo/conf/distributions << EOF
Origin: Sesterce CLI
Label: Sesterce CLI Repository
Codename: stable
Architectures: amd64 arm64 i386
Components: main
Description: Sesterce CLI Debian Package Repository
SignWith: default
EOF
    
    echo -e "${YELLOW}📋 Distributions file:${NC}"
    cat test-apt-repo/conf/distributions
    
    # Test adding package to repository
    echo -e "${YELLOW}🧪 Testing package addition to repository...${NC}"
    if reprepro -b test-apt-repo includedeb stable "$PACKAGE_FILE"; then
        echo -e "${GREEN}✅ Package added successfully${NC}"
        
        # Test export
        if reprepro -b test-apt-repo export stable; then
            echo -e "${GREEN}✅ Repository exported successfully${NC}"
            
            # Show repository contents
            echo -e "${YELLOW}📋 Repository contents:${NC}"
            reprepro -b test-apt-repo list stable
        else
            echo -e "${RED}❌ Repository export failed${NC}"
        fi
    else
        echo -e "${RED}❌ Failed to add package to repository${NC}"
    fi
    
    # Clean up
    rm -rf test-apt-repo
fi

echo -e "${GREEN}✅ APT repository test completed!${NC}" 