import os
import re

base_dir = r"C:\xampp\htdocs\New_Stram_Site_web\src\Entity"

# Entity -> list of (field_name, orm_type, nullable)
entities_config = {
    "About.php": [
        ("description_en", "text", True),
        ("mission_en", "text", True),
        ("vision_en", "string', length: 600", True),
    ],
    "Accueil.php": [
        ("textbanner_en", "string", True),
        ("descriptionsociete_en", "string', nullable: true, length: 600", True),
        ("descriptionsocietestramsimple_en", "string', nullable: true, length: 600", True),
        ("descriptionsocietestramaenginner_en", "string', nullable: true, length: 600", True),
        ("descriptionsocietestramaffrique_en", "string', nullable: true, length: 600", True),
        ("descriptionsocietestramplus_en", "string', nullable: true, length: 600", True),
        ("textmap_en", "string", True),
    ],
    "Approach.php": [
        ("heroTitle_en", "string', length: 255", True),
        ("heroText_en", "text", True),
        ("introTitle_en", "string', length: 255", True),
        ("introText_en", "text", True),
        ("pillar1Title_en", "string', length: 255", True),
        ("pillar1Text_en", "text", True),
        ("pillar2Title_en", "string', length: 255", True),
        ("pillar2Text_en", "text", True),
        ("pillar3Title_en", "string', length: 255", True),
        ("pillar3Text_en", "text", True),
    ],
    "Blog.php": [
        ("titre_en", "string", True),
        ("description_en", "text", True),
    ],
    "Sustainability.php": [
        ("headerTitle_en", "string', length: 255", True),
        ("headerDescription_en", "text", True),
        ("ecologyTitle_en", "string', length: 255", True),
        ("ecologyText_en", "text", True),
        ("socialTitle_en", "string', length: 255", True),
        ("socialText_en", "text", True),
        ("governanceTitle_en", "string', length: 255", True),
        ("governanceText_en", "text", True),
    ],
    "Temoignage.php": [
        ("description_en", "text", True),
        ("posteclient_en", "string", True),
    ],
    "Carriere.php": [
        ("titreoffre_en", "string", True),
        ("textoffre_en", "text", True),
    ],
}

def camel_case(name):
    """Convert field_name to CamelCase for getter/setter: heroTitle_en -> HeroTitleEn"""
    parts = name.split('_')
    return ''.join(p.capitalize() for p in parts)

def generate_field_block(field_name, orm_type, nullable):
    """Generate the ORM field declaration + getter + setter PHP code."""
    # Build the ORM Column annotation
    if "'" in orm_type:
        # Complex type already has quotes
        col = f"    #[ORM\\Column(type: '{orm_type}, nullable: true)]\n"
    else:
        col = f"    #[ORM\\Column(type: '{orm_type}', nullable: true)]\n"
    
    prop = f"    private ?string ${field_name} = null;\n\n"
    
    cc = camel_case(field_name)
    
    getter = f"    public function get{cc}(): ?string {{ return $this->{field_name}; }}\n"
    setter = f"    public function set{cc}(?string ${field_name}): self {{ $this->{field_name} = ${field_name}; return $this; }}\n\n"
    
    return col + prop + getter + setter

for filename, fields in entities_config.items():
    filepath = os.path.join(base_dir, filename)
    
    if not os.path.exists(filepath):
        print(f"SKIP (not found): {filepath}")
        continue
    
    with open(filepath, 'r', encoding='utf-8') as f:
        content = f.read()
    
    # Check if already modified
    first_field = fields[0][0]
    if first_field in content:
        print(f"SKIP (already has _en fields): {filename}")
        continue
    
    # Generate all field blocks
    new_code = "\n    // ===== English Translation Fields =====\n\n"
    for field_name, orm_type, nullable in fields:
        new_code += generate_field_block(field_name, orm_type, nullable)
    
    # Insert before the last closing brace
    last_brace = content.rfind('}')
    if last_brace == -1:
        print(f"ERROR: No closing brace found in {filename}")
        continue
    
    content = content[:last_brace] + new_code + "}\n"
    
    with open(filepath, 'w', encoding='utf-8') as f:
        f.write(content)
    
    print(f"UPDATED: {filename}")

print("\nAll entities updated!")
