#!/usr/bin/env python3
from __future__ import annotations
import argparse
from pathlib import Path
import re

ROOT = Path(__file__).resolve().parents[1]
TEMPLATES = {
    'list': 'CrudListPage.vue.template',
    'form': 'CrudFormPage.vue.template',
    'detail': 'CrudDetailPage.vue.template',
    'review': 'CrudReviewPage.vue.template',
}

def pascal(value: str) -> str:
    parts = re.findall(r'[A-Za-z0-9]+', value)
    if not parts:
        raise ValueError('feature must contain letters or digits')
    return ''.join(x[:1].upper() + x[1:] for x in parts)

def main() -> int:
    parser = argparse.ArgumentParser(description='Scaffold a K-ArtSell standard UI screen without vendor imports.')
    parser.add_argument('--feature', required=True)
    parser.add_argument('--kind', choices=sorted(TEMPLATES), required=True)
    parser.add_argument('--title', required=True)
    parser.add_argument('--force', action='store_true')
    args = parser.parse_args()

    feature = pascal(args.feature)
    template = (ROOT / 'templates/vue/screens' / TEMPLATES[args.kind]).read_text(encoding='utf-8')
    output = ROOT / 'frontend/src/features' / re.sub(r'(?<!^)(?=[A-Z])', '-', feature).lower() / 'pages' / f'{feature}Page.vue'
    if output.exists() and not args.force:
        raise SystemExit(f'refusing to overwrite {output.relative_to(ROOT)}; pass --force')
    output.parent.mkdir(parents=True, exist_ok=True)
    output.write_text(template.replace('__TITLE__', args.title).replace('__CONTRACT_VERSION__', 'UI-CONTRACT-2.0'), encoding='utf-8')
    print(output.relative_to(ROOT))
    return 0

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