from pathlib import Path
import re
import shutil
import sys

if len(sys.argv) != 2:
    raise SystemExit("Einen absoluten Studio-Datenpfad angeben.")
data = Path(sys.argv[1])
if not data.is_absolute():
    raise SystemExit("Der Datenpfad muss absolut sein.")
template = data / '03_TEMPLATES/PROJECT_TEMPLATE'
projects = data / '04_PROJECTS'

if not data.is_dir():
    raise SystemExit('Arbeits-SSD fehlt.')
if not template.is_dir() or not projects.is_dir():
    raise SystemExit('Vorlage oder Projektordner fehlt.')
if template.is_symlink() or any(
    p.is_symlink() for p in template.rglob('*')
):
    raise SystemExit('Vorlage enthaelt Verknuepfungen. Erst pruefen.')

name = input('Projektname, z.B. 2026_AD_Marke_Projekt: ').strip()
if not re.fullmatch(r'[A-Za-z0-9][A-Za-z0-9_-]{2,79}', name):
    raise SystemExit('3-80 Zeichen: Buchstaben, Ziffern, _ oder -.')

destination = projects / name
if destination.exists() or destination.is_symlink():
    raise SystemExit('Ziel existiert bereits. Nichts ueberschrieben.')
try:
    shutil.copytree(template, destination, dirs_exist_ok=False)
except OSError as error:
    raise SystemExit(
        f'Kopieren fehlgeschlagen: {error}. Teilordner pruefen.'
    )
print(f'Projekt angelegt: {destination}')
