#!/usr/bin/env python
"""Find all cloudflare.txt files in scraped site directories."""

from __future__ import annotations

import argparse
import sys
from pathlib import Path

DEFAULT_SCRAPED_PATH = Path(r"D:\tools\Scraper-geht-daheim\scraped")
OUTPUT_FILE = Path(__file__).resolve().parent / "cloudflare_test_sites.txt"


def _domain_from_path(cf_path: Path, root: Path) -> str:
    try:
        rel = cf_path.relative_to(root)
        return rel.parts[0]
    except (ValueError, IndexError):
        return cf_path.parent.name


def find_cloudflare_files(root: Path) -> list[tuple[str, Path]]:
    results = []
    for cf in sorted(root.rglob("cloudflare.txt")):
        domain = _domain_from_path(cf, root)
        results.append((domain, cf))
    return results


def main():
    p = argparse.ArgumentParser(description="Find sites blocked by Cloudflare")
    p.add_argument("--path", type=Path, default=DEFAULT_SCRAPED_PATH, help=f"Root scraped directory (default: {DEFAULT_SCRAPED_PATH})")
    p.add_argument("--urls-only", action="store_true", help="Print only domain names, one per line")
    p.add_argument("--show-content", action="store_true", help="Print file contents for each cloudflare.txt")
    args = p.parse_args()

    root = args.path.resolve()
    if not root.is_dir():
        print(f"ERROR: directory not found: {root}", file=sys.stderr)
        sys.exit(1)

    results = find_cloudflare_files(root)
    domains = [domain for domain, _ in results]

    if domains:
        OUTPUT_FILE.write_text("\n".join(domains) + "\n", encoding="utf-8")
        print(f"Wrote {len(domains)} domain(s) to {OUTPUT_FILE}")
    else:
        OUTPUT_FILE.write_text("", encoding="utf-8")

    if args.urls_only:
        for d in domains:
            print(d)
        return

    if not results:
        print(f"No cloudflare.txt files found in {root}")
        return

    for i, (domain, cf_path) in enumerate(results, 1):
        print(f"[{i:3d}] {domain}")
        print(f"      {cf_path}")
        if args.show_content:
            try:
                content = cf_path.read_text(encoding="utf-8").strip()
                if content:
                    for line in content.splitlines():
                        print(f"      | {line}")
            except Exception as exc:
                print(f"      | (read error: {exc})")
        print()

    print(f"Total: {len(results)} site(s) with Cloudflare protection")


if __name__ == "__main__":
    main()
