import typer
import requests

app = typer.Typer()

from bs4 import BeautifulSoup

# client.py
LOGIN_URL = "https://www.penracourses.org.uk/accounts/login"
# ADD_URL = 'http://localhost/add'


@app.command()
def login(user: str, password: str):
    rqst = requests.session()
    rsp = rqst.get(LOGIN_URL)

    token = BeautifulSoup(rsp.content, "html.parser").find("input").attrs["value"]#, attr={"name": "csrfmiddlewaretoken"}).attrs("value")

    #token = rsp.cookies["csrftoken"]
    #header = {"X-CSRFToken": token}
    #cookies = {"csrftoken": token}

    data = {
            "username": user,
            "password": password,  
            "csrfmiddlewaretoken": token,
            "next": "/",
        }

    print(data)
    rqst.headers["Referer"] = LOGIN_URL

    rsp = rqst.post(
        LOGIN_URL,
        data=data,
        #headers=header,
        #cookies=cookies
    )

    soup = BeautifulSoup(rsp.content, "html.parser")
    print(token)
    print(rqst.headers)
    print(rqst.cookies)

    login_success = False
    if soup.find("button") and soup.find("button").find(string="Login"):
        print("login fail")
        pass
    else:
        print("login success")
        login_success = True
    #print(rqst.headers)
    #print(rsp.content)

    #new = rqst.get(
    #    "https://www.penracourses.org.uk/api/atlas/get_cases_user"
    #)

    #print(new)
    #print(new.content)


if __name__ == "__main__":
    app()
