# retoor <retoor@molodetz.nl>

import hashlib
import time
from datetime import datetime, timedelta, timezone
from playwright.sync_api import expect
from tests.conftest import BASE_URL
from devplacepy.database import get_table
from devplacepy.utils import hash_password, generate_uid


def test_login_success(page, app_server):
    page.goto(f"{BASE_URL}/auth/signup", wait_until="domcontentloaded")
    page.fill("#username", "login_user")
    page.fill("#email", "login@test.devplace")
    page.fill("#password", "secret123")
    page.fill("#confirm_password", "secret123")
    page.fill("#birth_date", "1990-01-01")
    page.check("#accept_terms")
    page.click("button:has-text('Create account')")
    page.wait_for_url("**/feed", timeout=10000, wait_until="domcontentloaded")

    page.goto(f"{BASE_URL}/auth/logout", wait_until="domcontentloaded")

    page.goto(f"{BASE_URL}/auth/login", wait_until="domcontentloaded")
    page.fill("#email", "login@test.devplace")
    page.fill("#password", "secret123")
    page.click("button:has-text('Sign in')")
    page.wait_for_url("**/feed", timeout=10000, wait_until="domcontentloaded")
    assert page.is_visible("text=login_user")


def test_login_wrong_password(page, app_server):
    page.goto(f"{BASE_URL}/auth/signup", wait_until="domcontentloaded")
    page.fill("#username", "wrongpw_user")
    page.fill("#email", "wrongpw@test.devplace")
    page.fill("#password", "secret123")
    page.fill("#confirm_password", "secret123")
    page.fill("#birth_date", "1990-01-01")
    page.check("#accept_terms")
    page.click("button:has-text('Create account')")
    page.wait_for_url("**/feed", timeout=10000, wait_until="domcontentloaded")

    page.goto(f"{BASE_URL}/auth/logout", wait_until="domcontentloaded")

    page.goto(f"{BASE_URL}/auth/login", wait_until="domcontentloaded")
    page.fill("#email", "wrongpw@test.devplace")
    page.fill("#password", "badpassword")
    page.click("button:has-text('Sign in')")
    expect(page.locator("text=Invalid email or password")).to_be_visible()


def test_login_remember_me(page, app_server):
    page.goto(f"{BASE_URL}/auth/signup", wait_until="domcontentloaded")
    page.fill("#username", "remember_user")
    page.fill("#email", "remember@test.devplace")
    page.fill("#password", "secret123")
    page.fill("#confirm_password", "secret123")
    page.fill("#birth_date", "1990-01-01")
    page.check("#accept_terms")
    page.click("button:has-text('Create account')")
    page.wait_for_url("**/feed", timeout=10000, wait_until="domcontentloaded")

    page.goto(f"{BASE_URL}/auth/logout", wait_until="domcontentloaded")

    page.goto(f"{BASE_URL}/auth/login", wait_until="domcontentloaded")
    page.fill("#email", "remember@test.devplace")
    page.fill("#password", "secret123")
    page.check("input[name='remember_me']")
    page.click("button:has-text('Sign in')")
    page.wait_for_url("**/feed", timeout=10000, wait_until="domcontentloaded")
    assert page.is_visible("text=remember_user")


def test_logout(page, app_server):
    page.goto(f"{BASE_URL}/auth/signup", wait_until="domcontentloaded")
    page.fill("#username", "logout_user")
    page.fill("#email", "logout@test.devplace")
    page.fill("#password", "secret123")
    page.fill("#confirm_password", "secret123")
    page.fill("#birth_date", "1990-01-01")
    page.check("#accept_terms")
    page.click("button:has-text('Create account')")
    page.wait_for_url("**/feed", timeout=10000, wait_until="domcontentloaded")

    page.goto(f"{BASE_URL}/auth/logout", wait_until="domcontentloaded")
    page.wait_for_url("**/", wait_until="domcontentloaded")
    assert page.is_visible("text=Join DevPlace Free")
