#!/usr/bin/env python3
import os
import sys
import subprocess

def create_nextjs_site(project_name, business_name, business_type):
    base_dir = "/home/ubuntu/GhostAgency/sites"
    project_path = os.path.join(base_dir, project_name)
    
    os.makedirs(base_dir, exist_ok=True)
    
    print(f"[*] Inizializzazione cantiere Web Factory per '{business_name}'...")
    
    # Crea il progetto Next.js (richiede Node.js installato)
    # npx create-next-app@latest my-app --ts --tailwind --eslint --app --src-dir --import-alias "@/*" --use-npm
    cmd = f"npx -y create-next-app@latest {project_path} --javascript --tailwind --eslint --app --src-dir=false --import-alias='@/*' --use-npm"
    
    print(f"[*] Esecuzione comando: {cmd}")
    # Nota: su un server vero, questo comando impiega 1-2 minuti
    subprocess.run(cmd, shell=True, check=False)
    
    print("[*] Generazione del template B2B...")
    # Creiamo un file index.js base pesantemente ottimizzato per la conversione
    index_path = os.path.join(project_path, "app", "page.js")
    
    template = f"""
export default function Home() {{
  return (
    <main className="flex min-h-screen flex-col items-center justify-between p-24 bg-gray-900 text-white">
      <div className="z-10 max-w-5xl w-full items-center justify-between font-mono text-sm lg:flex">
        <h1 className="text-6xl font-bold bg-clip-text text-transparent bg-gradient-to-r from-blue-400 to-emerald-400">
          {business_name}
        </h1>
      </div>
      <div className="relative flex place-items-center mt-12">
        <p className="text-2xl text-center max-w-2xl text-gray-300">
          Il miglior servizio di {business_type} della città. 
          Sito in costruzione dalla Ghost Agency.
        </p>
      </div>
      <div className="mt-12">
        <button className="px-8 py-4 bg-emerald-500 text-white rounded-full font-bold text-xl hover:bg-emerald-600 transition-all">
          Contattaci Subito
        </button>
      </div>
    </main>
  )
}}
"""
    
    if os.path.exists(os.path.join(project_path, "app")):
        with open(index_path, "w") as f:
            f.write(template)
            
    print(f"[+] SITO GENERATO CON SUCCESSO! Cartella: {project_path}")
    print("[+] J.A.R.V.I.S. ha completato la struttura base.")

if __name__ == "__main__":
    if len(sys.argv) < 4:
        print("Uso: python3 web_factory.py <nome_cartella> <nome_business> <tipo_business>")
        sys.exit(1)
        
    create_nextjs_site(sys.argv[1], sys.argv[2], sys.argv[3])
