from pathlib import Path
from datetime import datetime
import requests


def create_project(url: str, super_token: str, project_xml_path: Path) -> str:
    """Create a project for testing on redcapdemo.vanderbilt.edu
    This API method returns the token for the newly created project, which
    used for the integration tests
    """
    current_time = datetime.now().strftime("%m-%d %H:%M:%S")
    project_title = f"PyCap { project_xml_path.stem }: { current_time }"
    project_info = f"""<?xml version="1.0" encoding="UTF-8" ?>
    <item>
    <project_title>{project_title}</project_title>
    <purpose>0</purpose>
    </item>"""

    with open(project_xml_path, encoding="UTF-8") as proj_xml_file:
        project_data = proj_xml_file.read()

    res = requests.post(
        url=url,
        data={
            "token": super_token,
            "content": "project",
            "format": "xml",
            "data": project_info,
            "odm": project_data,
        },
    )

    return res.text