#!/usr/bin/env python3
"""Create a new collection item draft."""

from __future__ import annotations

import argparse
import re
from datetime import date
from pathlib import Path


ROOT = Path(__file__).resolve().parents[2]


TEMPLATES = {
    "jobs": """# Job: {title}

---
type: job
company:
role: {title}
location:
source_url: {url}
source_name:
source_quality: unknown
collected_at: {today}
posted_at:
first_seen_at: {today}
last_checked_at: {today}
snapshot_type: new
job_code:
previous_snapshot:
status: unknown
level:
team:
business_area:
employment_type:
salary:
skills:
  -
topics:
  -
models:
  -
related_papers:
  -
related_experiments:
  -
related_projects:
  -
relevance: medium
---

## Source Snapshot

- source:
- collected_at: {today}
- availability:

## Change Snapshot

- first_seen_at: {today}
- last_checked_at: {today}
- snapshot_type: new
- previous_snapshot:
- changed_fields:

## Raw JD Summary

-

## Extracted Signals

- hard requirements:
- preferred skills:
- hidden signals:
- business direction:

## Knowledge Links

- concepts:
- papers:
- experiments:
- projects:

## Gaps for Us

-
""",
    "papers": """# Paper: {title}

---
type: paper
title: {title}
authors:
year:
venue:
url: {url}
code_url:
source:
collected_at: {today}
status: queued
relevance: medium
topics:
  -
methods:
  -
benchmarks:
  -
models:
  -
datasets:
  -
related_concepts:
  -
related_jobs:
  -
related_experiments:
  -
related_projects:
  -
---

## One-line Takeaway

-

## Problem

-

## Core Idea

-

## Evidence

-

## Useful For Us

-

## Follow-up Experiments

-
""",
    "industry": """# Industry: {title}

---
type: industry
company:
team:
title: {title}
url: {url}
source_name:
source_type: other
source_quality: unknown
published_at:
collected_at: {today}
status: queued
topics:
  -
implementation_signals:
  -
product_area:
  -
models:
  -
tools:
  -
benchmarks:
  -
related_papers:
  -
related_jobs:
  -
related_experiments:
  -
related_projects:
  -
evidence_level: medium
relevance: medium
---

## One-line Takeaway

-

## What They Built or Claimed

-

## Technical Signals

- architecture:
- tool use:
- memory:
- evaluation:
- safety:
- deployment:
- data:

## Evidence Quality

-
""",
}


def slugify(text: str) -> str:
    text = text.lower().strip()
    text = re.sub(r"[^a-z0-9\u4e00-\u9fff]+", "-", text)
    text = re.sub(r"-+", "-", text).strip("-")
    return text or "untitled"


def main() -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument("collection", choices=sorted(TEMPLATES))
    parser.add_argument("slug")
    parser.add_argument("--title", default=None)
    parser.add_argument("--url", default="")
    parser.add_argument("--date", default=date.today().isoformat())
    args = parser.parse_args()

    title = args.title or args.slug.replace("-", " ").title()
    filename = f"{args.date}-{slugify(args.slug)}.md"
    path = ROOT / args.collection / "items" / filename
    if path.exists():
        raise SystemExit(f"refusing to overwrite existing file: {path}")
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(
        TEMPLATES[args.collection].format(title=title, url=args.url, today=args.date),
        encoding="utf-8",
    )
    print(path.relative_to(ROOT))
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
