"""Website Automation Service for API layer - orchestrates website creation.""" from typing import Dict, Any, Optional from loguru import logger import os import tempfile import json from fastapi import HTTPException # Import the actual automation service from services.onboarding.website_automation_service import WebsiteAutomationService as CoreAutomationService class WebsiteAutomationService: """API layer service for website automation operations.""" def __init__(self): logger.info("🔄 Initializing WebsiteAutomationService (API layer)...") self.core_service = CoreAutomationService() async def generate_preview_site( self, user_id: str, site_brief: Dict[str, Any], css: str ) -> Dict[str, Any]: """Generate a preview site for the user.""" try: logger.info(f"Generating preview site for user {user_id}") # For preview, we'll create a temporary HTML file # In production, this could be hosted on a preview server preview_html = self._generate_preview_html(site_brief, css) # Save to temporary file (in production, use proper hosting) preview_url = f"/preview/{user_id}/index.html" return { "preview_url": preview_url, "preview_root": f"/preview/{user_id}", "preview_files": ["index.html", "custom.css"], "preview_html": preview_html } except Exception as e: logger.error(f"Failed to generate preview site: {str(e)}") raise HTTPException(status_code=500, detail=f"Preview generation failed: {str(e)}") def _generate_preview_html(self, site_brief: Dict[str, Any], css: str) -> str: """Generate HTML preview from site brief and CSS.""" try: site_data = site_brief.get("site_brief", {}) business_name = site_data.get("business_name", "Your Business") tagline = site_data.get("tagline", "Your tagline here") # Get content plan content_plan = site_brief.get("content_plan", {}) required_pages = content_plan.get("required_pages", []) # Generate HTML html = f""" {business_name}

{business_name}

{tagline}

ALwrity Preview
{self._generate_page_content(required_pages)}
""" return html except Exception as e: logger.error(f"Failed to generate preview HTML: {str(e)}") return f"

Preview Error

{str(e)}

" def _generate_page_content(self, required_pages: list) -> str: """Generate HTML content for pages.""" if not required_pages: return """

Welcome to Your Website

This is a preview of your new website. The content will be generated based on your business information.

About Us

Learn more about your business and what makes you unique.

Services

Discover the services and products you offer to your customers.

Contact

Get in touch with you through various contact methods.

""" content_parts = [] for page in required_pages: page_name = page.get("page", "page").title() goal = page.get("goal", "") key_points = page.get("key_points", []) cta = page.get("cta", "Get Started") page_html = f"""

{page_name}

{goal}

""" if key_points: page_html += "
" for point in key_points: page_html += f"""

{point}

""" page_html += "
" if cta: page_html += f"""
""" page_html += "
" content_parts.append(page_html) return "".join(content_parts) async def generate_website( self, user_id: str, business_info: Dict[str, Any], niche: str, site_brief: Optional[Dict[str, Any]] = None, css: Optional[str] = None ) -> Dict[str, str]: """Generate and deploy a full website.""" try: logger.info(f"Generating website for user {user_id}") # Use the core automation service result = await self.core_service.generate_website( user_id=user_id, business_info=business_info, niche=niche, site_brief=site_brief, css=css ) return result except Exception as e: logger.error(f"Failed to generate website: {str(e)}") raise HTTPException(status_code=500, detail=f"Website generation failed: {str(e)}") def get_deployment_status(self, user_id: str) -> Dict[str, Any]: """Get the status of website deployment.""" try: # This would typically check the deployment status # For now, return a placeholder return { "status": "pending", "message": "Deployment status checking not yet implemented" } except Exception as e: logger.error(f"Failed to get deployment status: {str(e)}") return { "status": "error", "message": str(e) } # Singleton instance website_automation_service = WebsiteAutomationService()