"""Init command for DeepSweep."""

from pathlib import Path

import click

CURSORRULES_TEMPLATE = '''# Project Rules for AI Assistant
# Generated by DeepSweep - https://deepsweep.ai
# Run: deepsweep validate

## Project Context
- This is a {project_type} project
- Follow existing code style and patterns
- Respect .gitignore and do not access ignored files

## Security Boundaries
- Never execute shell commands without explicit approval
- Never access files outside the project directory
- Never make network requests without explicit approval
- Never modify system configuration files
- Always confirm before deleting files

## Code Quality
- Write tests for new functionality
- Follow existing naming conventions
- Add comments for complex logic
- Keep functions focused and small
'''

MCP_TEMPLATE = '''{
  "mcpServers": {
    "@modelcontextprotocol/server-filesystem@1.0.0": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-filesystem@1.0.0", "."],
      "autoApprove": false
    }
  }
}
'''


@click.command()
@click.option("--type", "project_type", type=click.Choice(["python", "javascript", "typescript", "rust", "go", "general"]), default="general")
@click.option("--include-mcp", is_flag=True, default=False)
@click.option("--force", is_flag=True, default=False)
def init(project_type: str, include_mcp: bool, force: bool):
    """Create secure AI assistant configuration templates."""
    cwd = Path.cwd()

    rules_path = cwd / ".cursorrules"
    if rules_path.exists() and not force:
        click.echo(click.style(f"[SKIP] {rules_path.name} exists. Use --force to overwrite.", fg="yellow"))
    else:
        rules_path.write_text(CURSORRULES_TEMPLATE.format(project_type=project_type))
        click.echo(click.style(f"[OK] Created {rules_path.name}", fg="green"))

    if include_mcp:
        mcp_path = cwd / "mcp.json"
        if mcp_path.exists() and not force:
            click.echo(click.style(f"[SKIP] {mcp_path.name} exists. Use --force to overwrite.", fg="yellow"))
        else:
            mcp_path.write_text(MCP_TEMPLATE)
            click.echo(click.style(f"[OK] Created {mcp_path.name}", fg="green"))

    click.echo()
    click.echo("Next steps:")
    click.echo("  1. Review and customize the generated files")
    click.echo("  2. Run: deepsweep validate")
