# retoor <retoor@molodetz.nl>

import time
from playwright.sync_api import expect
from tests.conftest import BASE_URL
from devplacepy.database import get_table, refresh_snapshot


def test_admin_creates_backup_schedule_via_modal(alice):
    page, user = alice
    name = f"Nightly {int(time.time() * 1000)}"

    page.goto(f"{BASE_URL}/admin/backups", wait_until="domcontentloaded")
    page.locator("#backup-schedule-new").click()
    expect(page.locator("#backup-schedule-modal")).to_be_visible()

    page.locator("#backup-schedule-name").fill(name)
    page.locator("#backup-schedule-target").select_option("database")
    page.locator("#backup-schedule-kind").select_option("interval")
    page.locator("#backup-schedule-every").fill("3600")

    page.locator("#backup-schedule-form button[type='submit']").click()
    page.wait_for_url("**/admin/backups", wait_until="domcontentloaded")

    expect(page.locator(f"text={name}")).to_be_visible()

    refresh_snapshot()
    row = get_table("backup_schedules").find_one(name=name, deleted_at=None)
    assert row is not None
    assert row["target"] == "database"


def test_non_admin_cannot_open_backups_page(bob):
    page, user = bob
    page.goto(f"{BASE_URL}/admin/backups", wait_until="domcontentloaded")
    expect(page.locator("#backup-schedule-new")).to_have_count(0)
