#!/usr/bin/env python3
"""
Initialize a new OurDigital skill with standard directory structure.

Usage:
    python init_skill.py {skill-name} --number XX
    python init_skill.py blog --number 02
"""

import argparse
import os
from pathlib import Path
from datetime import datetime


SKILL_TEMPLATE = '''---
name: ourdigital-{name}
description: |
  {description}
  Activated with "ourdigital" keyword.

  Triggers:
  - "ourdigital {name}", "ourdigital {trigger}"

  Features:
  - {feature}
version: "1.0"
author: OurDigital
environment: {environment}
---

# OurDigital {title}

{description}

## Activation

Only activate when user includes "ourdigital" keyword:
- "ourdigital {trigger}"

## Workflow

### Phase 1: Input

Gather requirements from user.

### Phase 2: Process

Execute core functionality.

### Phase 3: Output

Deliver results.

## Quick Commands

| Command | Action |
|---------|--------|
| "ourdigital {name}" | Main action |
'''

CHANGELOG_TEMPLATE = '''# Changelog

All notable changes to this skill will be documented in this file.

## [1.0.0] - {date}

### Added
- Initial skill creation
- Desktop and Code versions
- Basic workflow implementation

### Notion Ref
- (To be added after sync)
'''

README_TEMPLATE = '''# OurDigital {title}

{description}

## Installation

This skill is part of the OurDigital custom skills package.

## Usage

### Claude Desktop

The skill activates when you mention "ourdigital {name}".

### Claude Code

```bash
# The skill activates via SKILL.md directive
```

## Structure

```
{number}-ourdigital-{name}/
├── code/SKILL.md
├── desktop/SKILL.md
├── shared/
│   └── references/
├── docs/
│   └── CHANGELOG.md
└── README.md
```

## Version

- Current: 1.0.0
- Author: OurDigital
'''


def create_skill(name: str, number: str, description: str = "", environment: str = "Both"):
    """Create a new skill directory structure."""

    base_path = Path(__file__).parent.parent.parent.parent
    skill_dir = base_path / f"{number}-ourdigital-{name}"

    if skill_dir.exists():
        print(f"Skill directory already exists: {skill_dir}")
        return False

    # Create directories
    dirs = [
        skill_dir / "code",
        skill_dir / "desktop",
        skill_dir / "shared" / "references",
        skill_dir / "shared" / "templates",
        skill_dir / "shared" / "scripts",
        skill_dir / "docs" / "logs",
    ]

    for d in dirs:
        d.mkdir(parents=True, exist_ok=True)
        print(f"Created: {d}")

    # Title case for display
    title = name.replace("-", " ").title()
    trigger = name.replace("-", " ")
    feature = f"Core {title} functionality"
    date = datetime.now().strftime("%Y-%m-%d")

    if not description:
        description = f"OurDigital {title} skill for Claude."

    # Create SKILL.md for desktop
    desktop_skill = SKILL_TEMPLATE.format(
        name=name,
        title=title,
        description=description,
        trigger=trigger,
        feature=feature,
        environment="Desktop"
    )
    (skill_dir / "desktop" / "SKILL.md").write_text(desktop_skill)
    print(f"Created: desktop/SKILL.md")

    # Create SKILL.md for code
    code_skill = SKILL_TEMPLATE.format(
        name=name,
        title=title,
        description=description,
        trigger=trigger,
        feature=feature,
        environment="Code"
    )
    (skill_dir / "code" / "SKILL.md").write_text(code_skill)
    print(f"Created: code/SKILL.md")

    # Create CHANGELOG.md
    changelog = CHANGELOG_TEMPLATE.format(date=date)
    (skill_dir / "docs" / "CHANGELOG.md").write_text(changelog)
    print(f"Created: docs/CHANGELOG.md")

    # Create README.md
    readme = README_TEMPLATE.format(
        title=title,
        name=name,
        number=number,
        description=description
    )
    (skill_dir / "README.md").write_text(readme)
    print(f"Created: README.md")

    print(f"\nSkill created successfully: {skill_dir}")
    return True


def main():
    parser = argparse.ArgumentParser(description="Initialize a new OurDigital skill")
    parser.add_argument("name", help="Skill name (e.g., 'blog', 'designer')")
    parser.add_argument("--number", required=True, help="Skill number (e.g., '02', '07')")
    parser.add_argument("--description", default="", help="Skill description")
    parser.add_argument("--environment", default="Both",
                        choices=["Desktop", "Code", "Both"],
                        help="Target environment")

    args = parser.parse_args()

    success = create_skill(
        name=args.name,
        number=args.number,
        description=args.description,
        environment=args.environment
    )

    return 0 if success else 1


if __name__ == "__main__":
    exit(main())
