#!/usr/bin/env python3
import sqlite3, json

conn = sqlite3.connect("/data/docker/sonarr/sonarr.db")
c = conn.cursor()

# Read current settings
row = c.execute("SELECT Id, Settings FROM Indexers WHERE Id = 1").fetchone()
if not row:
    print("Indexer 1 not found!")
    # List all indexers
    c.execute("SELECT Id, Name, Settings FROM Indexers")
    for r in c.fetchall():
        print(f"ID:{r[0]} Name:{r[1]}")
    conn.close()
    exit(1)

idx_id, settings_json = row
settings = json.loads(settings_json)
print(f"Current apiPath: {settings.get('apiPath')}")
print(f"Current apiKey: {settings.get('apiKey')}")

# Fix
settings["apiPath"] = "/1/api"
settings["apiKey"] = "6726d68050164df9928f33b59a43f7b6"

c.execute("UPDATE Indexers SET Settings = ? WHERE Id = ?", (json.dumps(settings), idx_id))
conn.commit()
print("Updated!")

# Verify
row2 = c.execute("SELECT Settings FROM Indexers WHERE Id = 1").fetchone()
if row2:
    s2 = json.loads(row2[0])
    print(f"apiPath: {s2.get('apiPath')}")
    print(f"apiKey: {s2.get('apiKey')}")

conn.close()
