from pymongo import MongoClient
from pymongo.errors import ServerSelectionTimeoutError

try:
    client = MongoClient(
        "mongodb://192.168.1.76:27017/",
        serverSelectionTimeoutMS=5000,
        connectTimeoutMS=5000
    )

    # Test pripojeni
    client.admin.command('ping')
    print("[OK] MongoDB pripojeni uspesne!")

    # Zobraz info
    print(f"Server info: {client.server_info()}")

    # Vytvor databazi
    db = client['fotky_buzalkovi']
    print(f"[OK] Databaze 'fotky_buzalkovi' vytvorena/existuje")

    # Vytvor kolekce s validaci
    db.create_collection(
        "photos",
        validator={
            "$jsonSchema": {
                "bsonType": "object",
                "required": ["file_name", "file_path"],
                "properties": {
                    "file_name": {"bsonType": "string"},
                    "file_path": {"bsonType": "string"},
                    "file_hash": {"bsonType": "string"},
                    "camera": {"bsonType": "string"},
                    "taken_at": {"bsonType": "date"},
                    "width": {"bsonType": "int"},
                    "height": {"bsonType": "int"},
                    "file_size": {"bsonType": "int"},
                    "exif": {"bsonType": "object"},
                    "tags": {"bsonType": "array"}
                }
            }
        }
    )
    print("[OK] Kolekce 'photos' vytvorena")

    # Vytvor indexy
    db.photos.create_index("file_hash", unique=True)
    db.photos.create_index("taken_at")
    db.photos.create_index("camera")
    db.photos.create_index("tags")
    print("[OK] Indexy vytvoreny")

    client.close()

except ServerSelectionTimeoutError as e:
    print(f"[ERROR] Timeout - MongoDB neni dostupny: {e}")
except Exception as e:
    print(f"[ERROR] {e}")
