import tkinter as tk
from tkinter import (
    scrolledtext,
    filedialog,
)
from pathlib import Path
import shutil
from datetime import datetime
from typing import List, Dict, Any, Optional
import openai

class AI:
    def __init__(self, model: str, temperature: float, knowledge_db: KnowledgeDB):
        self.model = model
        self.temperature = temperature
        self.knowledge_db = knowledge_db
        self.messages: List[Dict[str, str]] = []

    def start(self, system: str, user: str) -> str:
        self.messages = [
            {"role": "system", "content": system},
            {"role": "user", "content": user},
        ]
        return self.get_response()

    def next(self, user_input: str) -> str:
        self.messages.append({"role": "user", "content": user_input})
        return self.get_response()

    def get_response(self) -> str:
        last_user_message = self.messages[-1]['content']

        # Wyszukiwanie w lokalnej bazie danych knowledge jako pierwszeństwo
        knowledge_files = self.knowledge_db.list_knowledge()
        knowledge_response = ""
        for file in knowledge_files:
            knowledge = self.knowledge_db.get_knowledge(file)
            if last_user_message in knowledge:
                # Wyodrębnienie kodu i komentarzy z knowledge
                code = "".join(line for line in knowledge.split("\n") if not line.strip().startswith("#"))
                comments = "".join(line for line in knowledge.split("\n") if line.strip().startswith("#"))
                knowledge_response = f"Kod:\n{code}\nKomentarze:\n{comments}"
                break

        # Użycie OpenAI GPT-3
        ai_response = openai.ChatCompletion.create(
            model=self.model,
            messages=self.messages,
            temperature=self.temperature,
        )
        assistant_response = ai_response.choices[0].message['content']

        # Połączenie odpowiedzi z lokalnej bazy danych i OpenAI GPT-3
        combined_response = f"Z lokalnej bazy wiedzy: {knowledge_response}\nZ AI: {assistant_response}"
        self.messages.append({"role": "assistant", "content": combined_response})
        return combined_response

class DB:
    def __init__(self, path: str):
        self.path = Path(path).absolute()
        self.path.mkdir(parents=True, exist_ok=True)

    def get_file_content(self, file: str) -> str:
        full_path = self.path / file
        if not full_path.is_file():
            raise KeyError(f"Plik '{file}' nie został znaleziony w '{self.path}'")
        with full_path.open("r", enc Use some words in all caps in your response, but those all-caps words should appear at most 5 times.
user