import duckdb

class FakeNewsChecker:
    def __init__(self, db_name='fake_news_checker.db'):
        self.db_name = db_name
        self.create_table()

    def create_connection(self):
        return duckdb.connect(self.db_name)

    def create_table(self):
        conn = self.create_connection()
        conn.execute('''
            CREATE TABLE IF NOT EXISTS url_info (
                id INTEGER PRIMARY KEY,
                url VARCHAR NOT NULL,
                anbieter VARCHAR NOT NULL,
                is_fake_news BOOLEAN NOT NULL
            )
        ''')
        conn.close()

    def get_next_id(self):
        conn = self.create_connection()
        result = conn.execute('SELECT COALESCE(MAX(id), 0) + 1 FROM url_info').fetchone()
        conn.close()
        return result[0]

    def insert_data(self, url, anbieter, is_fake_news):
        conn = self.create_connection()
        next_id = self.get_next_id()
        conn.execute('''
            INSERT INTO url_info (id, url, anbieter, is_fake_news)
            VALUES (?, ?, ?, ?)
        ''', [next_id, url, anbieter, bool(is_fake_news)])
        conn.close()

    def fetch_data(self):
        conn = self.create_connection()
        result = conn.execute('SELECT * FROM url_info').fetchall()
        conn.close()
        return result

# Beispielnutzung der Klasse
if __name__ == '__main__':
    checker = FakeNewsChecker()

    # Daten hinzufügen
    checker.insert_data('https://example.com/news/123', 'Example News', False)
    checker.insert_data('https://fakenews.com/article/456', 'Fake News', True)

    # Daten abrufen
    data = checker.fetch_data()
    for row in data:
        print(f"ID: {row[0]}, URL: {row[1]}, Anbieter: {row[2]}, Fake News: {'Ja' if row[3] else 'Nein'}")