#!/usr/bin/env python3
"""Set up Homarr via Playwright: create admin user then configure OIDC."""

from playwright.sync_api import sync_playwright
import time


def main():
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=True)
        context = browser.new_context()
        page = context.new_page()

        # Step 1: Navigate to init page
        print("[1/5] Loading Homarr init page...")
        page.goto("http://100.92.88.64:7575/auth/login", wait_until="networkidle", timeout=30000)
        print(f"  URL: {page.url}")

        # Step 2: Click "Start from scratch"
        print("[2/5] Starting setup...")
        page.wait_for_timeout(2000)
        buttons = page.locator("button").all()
        for btn in buttons:
            t = btn.text_content()
            if t and "start" in t.lower():
                btn.click()
                break
        page.wait_for_timeout(3000)

        # Step 3: Create admin user
        print("[3/5] Creating admin user...")

        # Mantine renders inputs inside .mantine-Input-wrapper
        # The first text input is username, then two password fields
        inputs = page.locator("input:not([type='checkbox'])").all()
        if not inputs:
            # Mantine might use different elements
            inputs = page.locator("[data-mantine-input] input").all()
        if not inputs:
            inputs = page.locator(".mantine-TextInput-root input").all()

        print(f"  Found {len(inputs)} input elements")

        # Try filling by finding the actual input elements
        text_fields = page.locator('[data-mantine-input]').all()
        print(f"  Mantine input wrappers: {len(text_fields)}")

        # The simplest approach: type into the visible inputs
        # Fill username
        username_input = page.get_by_label("User", exact=False).first
        if username_input.count() > 0:
            username_input.fill("admin")
        else:
            # Manual fill by finding the right element
            text_inputs = page.locator('input:not([type="checkbox"]):not([type="password"])').all()
            if text_inputs:
                text_inputs[0].fill("admin")

        # Fill password
        pw_inputs = page.locator('input[type="password"]').all()
        for pw in pw_inputs:
            pw.fill("HomarrAdmin123!")

        # Click Create user
        submit_btn = page.get_by_role("button", name="Create user")
        if submit_btn.count() > 0:
            submit_btn.click()
        else:
            for btn in page.locator("button").all():
                t = btn.text_content()
                if t and "Create" in t:
                    btn.click()
                    break

        page.wait_for_timeout(5000)
        print(f"  URL after create: {page.url}")

        # Step 4: Navigate to settings to configure OIDC
        print("[4/5] Configuring OIDC...")
        page.goto("http://100.92.88.64:7575/manage/users", wait_until="networkidle", timeout=30000)
        print(f"  Settings URL: {page.url}")
        page.wait_for_timeout(5000)
        page.screenshot(path="/tmp/homarr_settings.png")

        # Step 5: Click on Authentication and configure OIDC
        print("[5/5] Final check...")
        page.screenshot(path="/tmp/homarr_final.png", full_page=True)
        print("  Screenshots saved to /tmp/homarr_*.png")

        browser.close()


if __name__ == "__main__":
    main()
