{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# README Example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from abc import ABC, abstractmethod\n",
    "from typing import Any\n",
    "\n",
    "from pyjudo import ServiceContainer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define service interfaces\n",
    "class IDatabaseConnection(ABC):\n",
    "    @abstractmethod\n",
    "    def query(self, sql: str) -> Any: ...\n",
    "\n",
    "class IDatabaseURIBuilder(ABC):\n",
    "    @abstractmethod\n",
    "    def get_uri(self) -> str: ..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Implement the services\n",
    "class DatabaseConnection(IDatabaseConnection):\n",
    "    def __init__(self, uri_builder: IDatabaseURIBuilder, table_name=\"default\"):\n",
    "        self.connection_string = uri_builder.get_uri()\n",
    "        self.table_name = table_name\n",
    "        self.connected = True\n",
    "        print(f\"Connected to database: {self.connection_string}\")\n",
    "\n",
    "    def query(self, sql: str) -> Any:\n",
    "        if not self.connected:\n",
    "            raise Exception(\"Not connected to the database.\")\n",
    "        print(f\"Executing query: {sql} FROM {self.table_name}\")\n",
    "        return {\"result\": \"data\"}\n",
    "\n",
    "    def dispose(self) -> None:\n",
    "        if self.connected:\n",
    "            self.connected = False\n",
    "            print(f\"Disconnected from database: {self.connection_string}\")\n",
    "\n",
    "class TestDatabaseURIBuilder(IDatabaseURIBuilder):\n",
    "    def get_uri(self):\n",
    "        return \"connect.to.me\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pyjudo.service_container.ServiceContainer at 0x240833e7590>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Create the service container\n",
    "services = ServiceContainer()\n",
    "\n",
    "# Register services\n",
    "services.add_transient(IDatabaseURIBuilder, TestDatabaseURIBuilder)\n",
    "services.add_scoped(IDatabaseConnection, DatabaseConnection)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Connected to database: connect.to.me\n",
      "Executing query: SELECT * FROM foobar\n",
      "Disconnected from database: connect.to.me\n",
      "{'result': 'data'}\n",
      "Database connection string: connect.to.me\n"
     ]
    }
   ],
   "source": [
    "with services.create_scope() as service_scope:\n",
    "    db = service_scope[IDatabaseConnection](table_name=\"foobar\")\n",
    "    result = db.query(\"SELECT *\")\n",
    "print(result)\n",
    "\n",
    "\n",
    "@services.inject\n",
    "def print_connection_str(db_uri_builder: IDatabaseURIBuilder):\n",
    "    print(\"Database connection string:\", db_uri_builder.get_uri())\n",
    "\n",
    "print_connection_str()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
