#!/usr/bin/env python3
"""Inject Supabase auth overlay into Command Sovereignty pages."""
import os
import re

BASE = '/home/vincent/projects/command-sovereignty/public'

AUTH_OVERLAY = '''
<!-- Auth Overlay -->
<div id="authModal" style="display:none;position:fixed;inset:0;z-index:9999;background:rgba(0,0,0,0.7);backdrop-filter:blur(8px);align-items:center;justify-content:center;">
  <div style="background:#111827;border:1px solid rgba(255,255,255,0.1);border-radius:1rem;padding:2rem;width:100%;max-width:400px;margin:1rem;position:relative;">
    <button onclick="document.getElementById('authModal').style.display='none'" style="position:absolute;top:1rem;right:1rem;background:none;border:none;color:#9ca3af;font-size:1.5rem;cursor:pointer;">&times;</button>
    <div id="authSignIn">
      <h2 style="font-size:1.5rem;font-weight:700;margin-bottom:0.5rem;color:#fff;">Welcome back</h2>
      <p style="color:#9ca3af;margin-bottom:1.5rem;font-size:0.875rem;">Sign in to Command Sovereignty</p>
      <div id="signInError" style="display:none;background:rgba(239,68,68,0.1);border:1px solid rgba(239,68,68,0.3);color:#fca5a5;padding:0.75rem;border-radius:0.5rem;font-size:0.875rem;margin-bottom:1rem;"></div>
      <input type="email" id="signInEmail" placeholder="Email" style="width:100%;padding:0.75rem 1rem;border-radius:0.5rem;border:1px solid rgba(255,255,255,0.15);background:rgba(255,255,255,0.05);color:#fff;font-size:0.875rem;margin-bottom:1rem;outline:none;">
      <input type="password" id="signInPassword" placeholder="Password" style="width:100%;padding:0.75rem 1rem;border-radius:0.5rem;border:1px solid rgba(255,255,255,0.15);background:rgba(255,255,255,0.05);color:#fff;font-size:0.875rem;margin-bottom:1rem;outline:none;">
      <button onclick="doSignIn()" style="width:100%;padding:0.75rem;border-radius:0.5rem;border:none;background:#00A846;color:#fff;font-weight:600;font-size:0.875rem;cursor:pointer;">Sign In</button>
      <p style="text-align:center;margin-top:1rem;font-size:0.875rem;color:#9ca3af;">Don't have an account? <a style="color:#00A846;cursor:pointer;text-decoration:none;" onclick="showSignup();return false;">Sign up</a></p>
    </div>
    <div id="authSignUp" style="display:none;">
      <h2 style="font-size:1.5rem;font-weight:700;margin-bottom:0.5rem;color:#fff;">Create account</h2>
      <p style="color:#9ca3af;margin-bottom:1.5rem;font-size:0.875rem;">Start commanding your business</p>
      <div id="signUpError" style="display:none;background:rgba(239,68,68,0.1);border:1px solid rgba(239,68,68,0.3);color:#fca5a5;padding:0.75rem;border-radius:0.5rem;font-size:0.875rem;margin-bottom:1rem;"></div>
      <input type="text" id="signUpName" placeholder="Full name" style="width:100%;padding:0.75rem 1rem;border-radius:0.5rem;border:1px solid rgba(255,255,255,0.15);background:rgba(255,255,255,0.05);color:#fff;font-size:0.875rem;margin-bottom:1rem;outline:none;">
      <input type="email" id="signUpEmail" placeholder="Email" style="width:100%;padding:0.75rem 1rem;border-radius:0.5rem;border:1px solid rgba(255,255,255,0.15);background:rgba(255,255,255,0.05);color:#fff;font-size:0.875rem;margin-bottom:1rem;outline:none;">
      <input type="password" id="signUpPassword" placeholder="Password (min 8 chars)" style="width:100%;padding:0.75rem 1rem;border-radius:0.5rem;border:1px solid rgba(255,255,255,0.15);background:rgba(255,255,255,0.05);color:#fff;font-size:0.875rem;margin-bottom:1rem;outline:none;">
      <button onclick="doSignUp()" style="width:100%;padding:0.75rem;border-radius:0.5rem;border:none;background:#00A846;color:#fff;font-weight:600;font-size:0.875rem;cursor:pointer;">Create Account</button>
      <p style="text-align:center;margin-top:1rem;font-size:0.875rem;color:#9ca3af;">Already have an account? <a style="color:#00A846;cursor:pointer;text-decoration:none;" onclick="showSignIn();return false;">Sign in</a></p>
    </div>
  </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2/dist/umd/supabase.js"></script>
<script>
(function(){
  var sb=supabase.createClient('https://tbomwpzqpbujqhriqzra.supabase.co','sb_publishable_DTwXPZUigdTML4YhCzoa3w_4awB0i7W');
  function openAuth(){document.getElementById('authModal').style.display='flex';showSignIn();}
  function closeAuth(){document.getElementById('authModal').style.display='none';document.getElementById('signInError').style.display='none';document.getElementById('signUpError').style.display='none';}
  function showSignIn(){document.getElementById('authSignIn').style.display='block';document.getElementById('authSignUp').style.display='none';document.getElementById('signInError').style.display='none';}
  function showSignup(){document.getElementById('authSignIn').style.display='none';document.getElementById('authSignUp').style.display='block';document.getElementById('signUpError').style.display='none';}
  function showErr(id,msg){var e=document.getElementById(id);e.textContent=msg;e.style.display='block';}
  window.doSignIn=async function(){
    var e=document.getElementById('signInEmail').value,p=document.getElementById('signInPassword').value;
    if(!e||!p){showErr('signInError','Please fill in all fields');return;}
    var r=await sb.auth.signInWithPassword({email:e,password:p});
    if(r.error){showErr('signInError',r.error.message);}else{window.location.href='/dashboard.html';}
  };
  window.doSignUp=async function(){
    var n=document.getElementById('signUpName').value,e=document.getElementById('signUpEmail').value,p=document.getElementById('signUpPassword').value;
    if(!e||!p){showErr('signUpError','Please fill in all fields');return;}
    if(p.length<8){showErr('signUpError','Password must be at least 8 characters');return;}
    var r=await sb.auth.signUp({email:e,password:p,options:{data:{display_name:n||e}}});
    if(r.error){showErr('signUpError',r.error.message);}else{showSignIn();alert('Account created! Check email for confirmation.');}
  };
  // Intercept login/signup clicks
  document.addEventListener('click',function(ev){
    var t=ev.target;
    if(t.href&&(t.href.indexOf('login.html')>=0||t.href.indexOf('signup.html')>=0||t.href.indexOf('signup?plan=')>=0)){ev.preventDefault();openAuth();}
    if(t.textContent&&(t.textContent.indexOf('Log in')>=0||t.textContent.indexOf('Sign up')>=0)){ev.preventDefault();openAuth();}
  });
  // Close on backdrop
  document.addEventListener('click',function(ev){if(ev.target.id==='authModal')closeAuth();});
})();
</script>
'''

# Pages to patch (all except dashboard.html)
pages = [
    'index.html',
    'login.html',
    'signup.html',
    'signup?plan=command.html',
    'signup?plan=growth.html',
    'signup?plan=launch.html',
    'docs/integrations.html',
    'for/enterprise.html',
    'for/multi-location.html',
    'for/single-location.html',
]

for page in pages:
    filepath = os.path.join(BASE, page)
    if not os.path.exists(filepath):
        print(f"  ✗ SKIP (not found): {page}")
        continue
    
    with open(filepath, 'r') as f:
        html = f.read()
    
    # Check if already patched
    if 'authModal' in html:
        print(f"  ✓ ALREADY PATCHED: {page}")
        continue
    
    # Inject auth overlay before </body>
    html = html.replace('</body>', AUTH_OVERLAY + '\n</body>')
    
    with open(filepath, 'w') as f:
        f.write(html)
    
    print(f"  ✓ PATCHED: {page}")

print("\nDone. All pages have auth overlay injected.")