#!/bin/bash

# StreamFlow IPTV - Build and Run Script

echo "🎬 StreamFlow IPTV - Starting build process..."

# Check if Docker is installed
if ! command -v docker &> /dev/null; then
    echo "❌ Docker is not installed. Please install Docker first."
    exit 1
fi

# Check if Docker Compose is installed
if ! command -v docker-compose &> /dev/null; then
    echo "❌ Docker Compose is not installed. Please install Docker Compose first."
    exit 1
fi

# Create .env file if it doesn't exist
if [ ! -f .env ]; then
    echo "📝 Creating .env file from template..."
    cp .env.example .env
    echo "⚠️  Please update JWT_SECRET and SESSION_SECRET in .env file before running in production!"
fi

# Build and start the containers
echo "🏗️  Building Docker containers..."
docker-compose build

if [ $? -ne 0 ]; then
    echo "❌ Docker build failed. Please check the errors above."
    exit 1
fi

echo "🚀 Starting StreamFlow application..."
docker-compose up -d

if [ $? -ne 0 ]; then
    echo "❌ Failed to start containers. Please check the errors above."
    exit 1
fi

echo ""
echo "✅ StreamFlow is now running!"
echo ""
echo "🌐 Access the application at: http://localhost:12345"
echo ""
echo "📊 View logs with: docker-compose logs -f"
echo "🛑 Stop the application: docker-compose down"
echo ""
echo "🔐 Don't forget to register your first user account!"
