from pathlib import Path
import json, sys

root = Path(sys.argv[1] if len(sys.argv) > 1 else ".").resolve()
project = root / ".agents" / "PROJECT.md"

dotnet = list(root.glob("*.sln")) + list(root.glob("**/*.csproj"))
node = (root / "package.json").exists()
flutter = (root / "pubspec.yaml").exists()
docker = (root / "Dockerfile").exists() or bool(list(root.glob("docker-compose*.y*ml")))

commands = []
if dotnet:
    commands += ["dotnet build", "dotnet test"]
if node:
    try:
        package = json.loads((root / "package.json").read_text(encoding="utf-8"))
        scripts = package.get("scripts", {})
        commands += [f"npm run {x}" for x in ("lint","typecheck","test","build") if x in scripts]
    except Exception:
        commands += ["npm install"]
if flutter:
    commands += ["flutter pub get", "flutter analyze", "flutter test"]

content = f"""# Project Context

## Identity
- Name: {root.name}
- Purpose: UNKNOWN
- Repository type: Auto-detected; refine manually

## Technology
- Backend: {"ASP.NET Core/.NET" if dotnet else "UNKNOWN"}
- Frontend: {"Node/React/Vite/Next candidate" if node else "UNKNOWN"}
- Mobile: {"Flutter" if flutter else "UNKNOWN"}
- Database: UNKNOWN
- Infrastructure: {"Docker detected" if docker else "UNKNOWN"}

## Architecture
- Pattern: UNKNOWN
- Entry points: UNKNOWN
- Module boundaries: UNKNOWN
- External integrations: UNKNOWN

## Commands
{chr(10).join(f"- Candidate: `{c}`" for c in commands) if commands else "- UNKNOWN"}

## Constraints
- Security/privacy: UNKNOWN
- Compatibility: UNKNOWN
- Deployment target: UNKNOWN
"""
project.write_text(content, encoding="utf-8")
print(f"Initialized {project}")
