"""Example: Using the Links service to create and manage short URLs."""

from sed_sh import SedSH

# Initialize client (API key from environment variable SEDSH_API_KEY)
client = SedSH()

# Example 1: Create a simple short URL
print("=== Example 1: Create short URL ===")
link = client.links.create("https://github.com/anthropics/claude-code")
print(f"Short URL: {link['shortUrl']}")
print(f"Code: {link['code']}")
print(f"Target: {link['targetUrl']}")
print()

# Example 2: Create a password-protected short URL
print("=== Example 2: Password-protected URL ===")
protected_link = client.links.create(
    "https://docs.sed.sh",
    password="secret123"
)
print(f"Short URL: {protected_link['shortUrl']}")
print(f"Password required: {protected_link.get('passwordProtected', False)}")
print()

# Example 3: List all links
print("=== Example 3: List all links ===")
links = client.links.list()
print(f"Total links: {len(links['links'])}")
for link in links['links'][:3]:  # Show first 3
    print(f"  - {link['code']}: {link['targetUrl']} (clicks: {link.get('clicks', 0)})")
print()

# Example 4: Delete a link
print("=== Example 4: Delete a link ===")
# Delete the first link we created
result = client.links.delete(link['code'])
print(f"Deleted: {result}")

# Example 5: Using context manager
print("=== Example 5: Context manager ===")
with SedSH() as client:
    link = client.links.create("https://www.sed.sh")
    print(f"Created: {link['shortUrl']}")
# Session automatically closed
