from pathlib import Path
import re
import shutil
import sys

if len(sys.argv) != 2:
    raise SystemExit("Provide an absolute studio-data path.")
data = Path(sys.argv[1])
if not data.is_absolute():
    raise SystemExit("The data path must be absolute.")
template = data / '03_TEMPLATES/PROJECT_TEMPLATE'
projects = data / '04_PROJECTS'

if not data.is_dir():
    raise SystemExit('Working SSD is missing.')
if not template.is_dir() or not projects.is_dir():
    raise SystemExit('Template or project folder is missing.')
if template.is_symlink() or any(
    p.is_symlink() for p in template.rglob('*')
):
    raise SystemExit('Template contains links. Check first.')

name = input('Project name, e.g. 2026_AD_Brand_Project: ').strip()
if not re.fullmatch(r'[A-Za-z0-9][A-Za-z0-9_-]{2,79}', name):
    raise SystemExit('3-80 characters: letters, digits, _ or -.')

destination = projects / name
if destination.exists() or destination.is_symlink():
    raise SystemExit('Destination exists. Nothing overwritten.')
try:
    shutil.copytree(template, destination, dirs_exist_ok=False)
except OSError as error:
    raise SystemExit(
        f'Copy failed: {error}. Check the partial folder.'
    )
print(f'Project created: {destination}')
