
import pandas as pd

def apply_verification_fixes():
    csv_path = 'leads/leads.csv'
    df = pd.read_csv(csv_path)
    
    # Helper to update row based on Innung name
    def update_lead(innung, new_email=None, new_contact=None, new_phone=None, new_address=None):
        mask = df['Firm/Innung'].str.contains(innung, case=False, na=False)
        if mask.any():
            if new_email: df.loc[mask, 'Email'] = new_email
            if new_contact: df.loc[mask, 'Contact Person'] = new_contact
            if new_phone: df.loc[mask, 'Phone'] = new_phone
            if new_address: df.loc[mask, 'Address'] = new_address
            print(f"Updated {innung}")

    # --- Schweinfurt Fixes ---
    # Bäckerinnung Schweinfurt - Haßberge -> Fusioniert? Keeping KH contact but noting it might be Mainfranken now.
    # Actually, let's keep the KH contact if it's the only one, but maybe update the name if we found a better one?
    # Search said: "Bäckerinnung Schweinfurt-Haßberge... fusioniert... zur neuen Bäcker-Innung Mainfranken"
    # So we should probably rename it or at least acknowledge it. checking Mainfranken entry?
    # For now, let's Stick to the specific Obermeister emails we found.
    
    # Friseurinnung Main-Rhön -> Margit Rosentritt? Search said website is www.friseurinnung-main-rhoen.de
    # Email: info@friseurinnung-main-rhoen.de (Guess? Website didn't explicitly say email, but likely).
    # Re-reading search: "Adresse: Galgenleite 3... E-Mail: rapp@kreishandwerkerschaft-sw.de". 
    # So the KH email IS the official one for the Innung.
    
    # Innung für Land- und Baumaschinentechnik Unterfranken
    update_lead("Innung für Land- und Baumaschinentechnik Unterfranken", new_email="info@innung-landbautechnik.de")

    # Malerinnung Schweinfurt Stadt- und Land
    update_lead("Malerinnung Schweinfurt Stadt- und Land", new_email="info@malerinnung-schweinfurt.de")

    # Metallinnung Schweinfurt - Haßberge -> rapp@... confirmed.

    # Zimmerer-Innung Schweinfurt-Haßberge -> Reichhold. 
    # Search confirms KH manages it. But maybe add Reichhold's email if available? 
    # Only found phone. Keeping KH email but maybe adding Obermeister name if missing.
    update_lead("Zimmerer-Innung Schweinfurt-Haßberge", new_contact="Marion Reichhold")

    # Steinmetz- und Steinbildhauerinnung Unterfranken
    update_lead("Steinmetz- und Steinbildhauerinnung Unterfranken", new_contact="Josef Hofmann", new_email="info@stein-welten.com")

    # Schreinerinnung Haßberge – Schweinfurt
    update_lead("Schreinerinnung Haßberge – Schweinfurt", new_contact="Horst Zitterbart", new_email="schreinerei.zitterbart@t-online.de")

    # --- Bad Kissingen Fixes ---
    # Bauinnung Bad Kissingen / Rhön-Grabfeld
    # Stefan Goos. Email not explicitly found, but "info@bauunternehmen-zehe.de" is likely for his company.
    # Safe bet: Keep KH or try to find his specific one? 
    # Let's stick to KH if we are unsure, but maybe update Contact Person.
    update_lead("Bauinnung Bad Kissingen / Rhön-Grabfeld", new_contact="Stefan Goos")

    # Metall-Innung Bad Kissingen/Rhön-Grabfeld
    update_lead("Metall-Innung Bad Kissingen/Rhön-Grabfeld", new_contact="Klaus Engelmann", new_email="info@metallinnung-kg-nes.de")

    # Schreinerinnung Bad Kissingen
    update_lead("Schreinerinnung Bad Kissingen", new_contact="Norbert Borst", new_email="khw-kg@t-online.de") # Confirmed

    # Maler- und Lackiererinnung Bad Kissingen
    update_lead("Maler- und Lackiererinnung Bad Kissingen", new_contact="Mathias Stöth", new_email="khw-kg@t-online.de") # Confirmed

    # --- Freemail Fixes ---
    # Bau-Innung Schweinfurt -> Karl Böhner
    update_lead("Bau-Innung Schweinfurt", new_email="info@bauinnung-schweinfurt.de", new_contact="Karl Böhner")

    # Bauinnung Mainfranken - Würzburg -> Ralf Stegmeier
    update_lead("Bauinnung Mainfranken - Würzburg", new_email="info@trend-bau.com", new_contact="Ralf Stegmeier")

    df.to_csv(csv_path, index=False)
    print("Verification fixes applied.")

if __name__ == "__main__":
    apply_verification_fixes()
