#!/bin/bash

# Frontend deployment script for CloudFront + S3
# This script builds the Next.js app and deploys it to S3 + CloudFront

set -e

echo "🚀 Starting frontend deployment..."

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

# Configuration
S3_BUCKET="filamenteka-frontend"
REGION="eu-central-1"

# Get CloudFront distribution ID from terraform output
echo -e "${BLUE}📋 Getting CloudFront distribution ID...${NC}"
DISTRIBUTION_ID=$(terraform -chdir=terraform output -raw cloudfront_distribution_id 2>/dev/null || terraform output -raw cloudfront_distribution_id)

if [ -z "$DISTRIBUTION_ID" ]; then
    echo -e "${RED}❌ Could not get CloudFront distribution ID${NC}"
    exit 1
fi

echo -e "${GREEN}✓ Distribution ID: $DISTRIBUTION_ID${NC}"

# Build the Next.js app
echo -e "${BLUE}🔨 Building Next.js app...${NC}"
npm run build

if [ ! -d "out" ]; then
    echo -e "${RED}❌ Build failed - 'out' directory not found${NC}"
    exit 1
fi

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

# Upload to S3 with proper cache headers
echo -e "${BLUE}📤 Uploading to S3 with optimized cache headers...${NC}"

# First, delete old files
echo -e "${BLUE}  🗑️  Cleaning old files...${NC}"
aws s3 sync out/ s3://$S3_BUCKET/ \
  --region $REGION \
  --delete \
  --exclude "*"

# Upload HTML files with no-cache (always revalidate)
echo -e "${BLUE}  📄 Uploading HTML files (no-cache)...${NC}"
aws s3 sync out/ s3://$S3_BUCKET/ \
  --region $REGION \
  --exclude "*" \
  --include "*.html" \
  --cache-control "public, max-age=0, must-revalidate" \
  --content-type "text/html"

# Upload _next static assets with long-term cache (immutable, 1 year)
echo -e "${BLUE}  🎨 Uploading Next.js static assets (1 year cache)...${NC}"
aws s3 sync out/_next/ s3://$S3_BUCKET/_next/ \
  --region $REGION \
  --cache-control "public, max-age=31536000, immutable"

# Upload other static assets with moderate cache (1 day)
echo -e "${BLUE}  📦 Uploading other assets (1 day cache)...${NC}"
aws s3 sync out/ s3://$S3_BUCKET/ \
  --region $REGION \
  --exclude "*" \
  --exclude "*.html" \
  --exclude "_next/*" \
  --include "*" \
  --cache-control "public, max-age=86400"

echo -e "${GREEN}✓ All files uploaded to S3 with optimized cache headers${NC}"

# Invalidate CloudFront cache
echo -e "${BLUE}🔄 Invalidating CloudFront cache...${NC}"
INVALIDATION_ID=$(aws cloudfront create-invalidation \
  --distribution-id $DISTRIBUTION_ID \
  --paths "/*" \
  --query 'Invalidation.Id' \
  --output text)

echo -e "${GREEN}✓ CloudFront invalidation created: $INVALIDATION_ID${NC}"

# Get CloudFront URL
CF_URL=$(terraform -chdir=terraform output -raw cloudfront_domain_name 2>/dev/null || terraform output -raw cloudfront_domain_name)

echo ""
echo -e "${GREEN}✅ Deployment complete!${NC}"
echo -e "${BLUE}🌐 CloudFront URL: https://$CF_URL${NC}"
echo -e "${BLUE}🌐 Custom Domain: https://filamenteka.rs (after DNS update)${NC}"
echo ""
echo -e "${BLUE}ℹ️  Note: CloudFront cache invalidation may take 5-10 minutes to propagate globally.${NC}"
