#!/bin/bash

# Easypanel Deployment Script for MoreminiMore Redesign
# This script helps deploy to Easypanel

set -e

# Configuration
GITEA_URL="https://git.moreminimore.com/kunthawat/moreminimore-website.git"
DOCKERFILE_PATH="Dockerfile"
CONTAINER_PORT=4321
PROJECT_NAME="moreminimore-website"

echo "=========================================="
echo "MoreminiMore Redesign - Easypanel Deploy"
echo "=========================================="
echo ""

# Step 1: Check if Docker is installed
echo "Step 1: Checking Docker..."
if command -v docker &> /dev/null; then
    echo "✓ Docker is installed"
    docker --version
else
    echo "✗ Docker is not installed"
    echo "Please install Docker Desktop: https://www.docker.com/products/docker-desktop/"
    exit 1
fi

# Step 2: Build Docker image locally (test)
echo ""
echo "Step 2: Building Docker image locally..."
cd moreminimore-redesign
docker build -t moreminimore-redesign:latest .

if [ $? -eq 0 ]; then
    echo "✓ Docker build successful"
else
    echo "✗ Docker build failed"
    exit 1
fi

# Step 3: Test locally
echo ""
echo "Step 3: Testing locally (optional)..."
echo "To test locally, run:"
echo "  docker run -p 4321:4321 moreminimore-redesign:latest"
echo "Then visit: http://localhost:4321"
echo ""
read -p "Skip local test and continue? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    docker run -p 4321:4321 moreminimore-redesign:latest &
    echo "Server started. Press Ctrl+C to stop and continue deployment."
    wait
fi

# Step 4: Push to Gitea
echo ""
echo "Step 4: Pushing to Gitea..."
git add .
git commit -m "Deploy: $(date '+%Y-%m-%d %H:%M:%S')" || echo "No changes to commit"
git push origin main

if [ $? -eq 0 ]; then
    echo "✓ Code pushed to Gitea"
else
    echo "✗ Failed to push to Gitea"
    exit 1
fi

# Step 5: Easypanel Instructions
echo ""
echo "=========================================="
echo "Step 5: Configure Easypanel"
echo "=========================================="
echo ""
echo "Manual Steps Required:"
echo ""
echo "1. Login to your Easypanel instance"
echo ""
echo "2. Create or select project:"
echo "   - Project Name: moreminimore-website"
echo ""
echo "3. Add New Service:"
echo "   - Type: Git Repository"
echo "   - Repository URL: $GITEA_URL"
echo "   - Branch: main"
echo ""
echo "4. Configure Docker:"
echo "   - Dockerfile: $DOCKERFILE_PATH"
echo "   - Port: $CONTAINER_PORT"
echo ""
echo "5. Enable Auto-Deploy:"
echo "   - Settings → Auto-deploy → Enable"
echo "   - Branch: main"
echo ""
echo "6. Deploy!"
echo "   - Easypanel will build and deploy automatically"
echo ""
echo "=========================================="
echo "Deployment script completed!"
echo "=========================================="
