import requests
import json
from bs4 import BeautifulSoup
import csv
import os
import time
import smtplib
from email.mime.text import MIMEText
import ssl

# File to store already processed links
FILENAME = "wohnungen_2025.csv"
FIELDNAMES = ["URL"]

# Ensure the CSV file exists
def ensure_csv_file():
    if not os.path.exists(FILENAME):
        with open(FILENAME, "w", newline="") as file:
            writer = csv.DictWriter(file, fieldnames=FIELDNAMES)
            writer.writeheader()

# Check if a link has already been processed
def wohnung_found(link):
    ensure_csv_file()
    with open(FILENAME, "r", newline="") as file:
        reader = csv.DictReader(file)
        if any(row["URL"] == link for row in reader):
            return True

    # If not found, add it to the file
    with open(FILENAME, "a", newline="") as file:
        writer = csv.DictWriter(file, fieldnames=FIELDNAMES)
        writer.writerow({"URL": link})
    return False

# Send an email notification
def send_message(wohnung):
    sender_email = "test@amakaa.de"
    recipients = [
        "Sahar.sellami@gmail.com",
        "abu.hmaed3@gmail.com",
        "amine.sellamii@gmail.com",
        "linda.dhahbi@yahoo.fr",
    ]
    subject = "Achtung Wohnung"
    smtp_server = "mail.amakaa.de"
    smtp_port = 465
    smtp_username = sender_email
    smtp_password = "Gusthant123"  # Replace with a secure method for storing passwords

    message = MIMEText(json.dumps(wohnung, indent=4))
    message["Subject"] = subject
    message["From"] = sender_email
    message["To"] = ", ".join(recipients)

    context = ssl.create_default_context()

    try:
        with smtplib.SMTP_SSL(smtp_server, smtp_port, context=context) as server:
            server.login(smtp_username, smtp_password)
            for recipient in recipients:
                server.sendmail(sender_email, recipient, message.as_string())
        print(f"Email sent to: {', '.join(recipients)}")
    except Exception as e:
        print(f"Failed to send email: {e}")

# Main scraping function
def scrape_wohnungen():
    url = "https://inberlinwohnen.de/wp-content/themes/ibw/skript/wohnungsfinder.php"
    headers = {
        "accept": "*/*",
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "origin": "https://inberlinwohnen.de",
        "referer": "https://inberlinwohnen.de/wohnungsfinder/",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36",
        "x-requested-with": "XMLHttpRequest",
    }
    payload = {
        "q": "wf-save-srch",
        "save": "false",
        "bez[]": ["01_00", "02_00", "03_00", "04_00", "05_00", "06_00", "07_00", "08_00"],
        "balkon_loggia_terrasse": "true",
        "wbs": "0",
    }

    try:
        response = requests.post(url, headers=headers, data=payload)
        response.raise_for_status()
        response_json = response.json()

        searchresults_html = response_json.get("searchresults", "")
        soup = BeautifulSoup(searchresults_html, "html.parser")

        flats = soup.find_all("li", class_="tb-merkflat")

        for flat in flats:
            flat_details = flat.find("h3").get_text(separator="|").split("|")
            if len(flat_details) >= 7:
                rooms = flat_details[0].strip()
                area = flat_details[2].strip() + " " + flat_details[3].strip()
                rent = flat_details[4].strip() + " " + flat_details[5].strip()
                address = flat_details[6].strip()

                link_tag = flat.find("a", href=True)
                link = "https://inberlinwohnen.de" + link_tag["href"] if link_tag else "No link available"

                if not wohnung_found(link):
                    wohnung = {
                        "rooms": rooms,
                        "area": area,
                        "rent": rent,
                        "address": address,
                        "link": link,
                    }
                    send_message(wohnung)
    except requests.exceptions.RequestException as e:
        print(f"Request failed: {e}")
    except Exception as e:
        print(f"An error occurred: {e}")

# Run the scraper in a loop
if __name__ == "__main__":
    while True:
        scrape_wohnungen()
        print("Request Done")
        time.sleep(300)  # Wait 5 minutes before checking again
