import pathlib
import shutil
from typing import Dict, Any

def create_project_folders(project_path: pathlib.Path):
    """Creates the standard folder structure for an After Effects project."""
    folders = [
        "project",
        "footage/video",
        "footage/audio",
        "footage/renders",
        "assets/images",
        "assets/fonts",
        "assets/documents",
        "exports/drafts",
        "exports/final",
    ]
    for folder in folders:
        (project_path / folder).mkdir(parents=True, exist_ok=True)

def generate_jsx(project_path: pathlib.Path, project_name: str, config: Dict[str, Any]):
    """Generates an ExtendScript .jsx file with the project configuration."""
    # resolution is "1920×1080"
    res_parts = config["resolution"].split("×")
    width = int(res_parts[0])
    height = int(res_parts[1])
    
    fps = config["fps"]
    duration = config["duration"]
    
    jsx_content = f"""// Auto-generated by aeset — do not edit manually
var projectName = "{project_name}";
var width       = {width};
var height      = {height};
var fps         = {fps};
var duration    = {duration};

app.beginUndoGroup("Scaffold Project");

// 1. Create Folder Structure in AE Project Panel
var compFolder    = app.project.items.addFolder("_COMPS");
var footageFolder = app.project.items.addFolder("_FOOTAGE");
var assetsFolder  = app.project.items.addFolder("_ASSETS");
var audioFolder   = app.project.items.addFolder("_AUDIO");

// 2. Create the main Composition
var comp = app.project.items.addComp(
    projectName,
    width,
    height,
    1.0,
    duration,
    fps
);

// 3. Move comp into the COMPS folder
comp.parentFolder = compFolder;

comp.openInViewer();
app.endUndoGroup();

app.project.save();
"""
    jsx_path = project_path / "project" / "setup.jsx"
    jsx_path.write_text(jsx_content)
    return jsx_path

def copy_template_aep(project_path: pathlib.Path, project_name: str):
    """Copies the blank .aep template to the new project directory."""
    # it finds the template relative to this file
    template_path = pathlib.Path(__file__).parent / "templates" / "blank.aep"
    dest_path = project_path / "project" / f"{project_name}.aep"
    
    if not template_path.exists():
        # For development, if blank.aep isn't there, it create a placeholder
        template_path.parent.mkdir(parents=True, exist_ok=True)
        template_path.write_text("DUMMY AEP BINARY CONTENT")
        
    shutil.copy2(template_path, dest_path)
    return dest_path

def generate_readme(project_path: pathlib.Path, project_name: str, config: Dict[str, Any]):
    """Generates a README.md for the project with metadata."""
    readme_content = f"""# {project_name}

**Project Type:** {config["type"]}
**Resolution:** {config["resolution"]}
**Frame Rate:** {config["fps"]} fps
**Duration:** {config["duration"]} seconds
**Color Space:** {config["color_space"]}
**Created:** {config["date"]}

---
Auto-generated by aeset.
"""
    (project_path / "README.md").write_text(readme_content)
