
import sys
import os
import argparse

# Adjust the path to include the 'company-explorer' directory
# This allows the script to find the 'backend' module
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))

from backend.database import SessionLocal, Industry, Persona, MarketingMatrix

def check_texts_for_industry(industry_name: str):
    """
    Fetches and prints the marketing matrix texts for all personas
    within a specific industry.
    """
    db = SessionLocal()
    try:
        industry = db.query(Industry).filter(Industry.name == industry_name).first()

        if not industry:
            print(f"Error: Industry '{industry_name}' not found.")
            # Suggest similar names
            all_industries = db.query(Industry.name).all()
            print("\nAvailable Industries:")
            for (name,) in all_industries:
                print(f"- {name}")
            return

        entries = (
            db.query(MarketingMatrix)
            .join(Persona)
            .filter(MarketingMatrix.industry_id == industry.id)
            .order_by(Persona.id)
            .all()
        )

        if not entries:
            print(f"No marketing texts found for industry: {industry_name}")
            return

        print(f"--- NEW TEXTS FOR {industry_name} ---")
        for entry in entries:
            print(f"\nPERSONA: {entry.persona.name}")
            print(f"Subject: {entry.subject}")
            print(f"Intro: {entry.intro.replace(chr(10), ' ')}") # Replace newlines for cleaner one-line output

    except Exception as e:
        print(f"An error occurred: {e}")
    finally:
        db.close()

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Debug and check marketing matrix texts for a given industry.")
    parser.add_argument(
        "industry",
        type=str,
        nargs='?',
        default="Healthcare - Hospital",
        help="The name of the industry to check (e.g., 'Healthcare - Hospital'). Defaults to 'Healthcare - Hospital'."
    )
    args = parser.parse_args()
    
    check_texts_for_industry(args.industry)
