def process_ad_slots():
    
    from django.db import connection
    import requests
    from datetime import datetime
    # URL to fetch the playlist JSON
    url = "https://almatv-stor.vizionr.fr/synthesia/almatv/playlist/manager/php/_getPlaylist.php?fields=title,isPub,duration"

    # Function to fetch and parse the playlist JSON from the URL
    def fetch_playlist_data(url):
        response = requests.get(url)
        if response.status_code == 200:
            return response.json()
        else:
            return None
    

    def insert_ad_slots(ad_slots_json):
        # check if subid already in database skip it
        # check if data already has been saved
        
        cursor = connection.cursor()
        fetch_query = """
                    SELECT PlaylistDate FROM Adslots_VisionR;
                    """
        data = cursor.execute(fetch_query)
        print(data)
        
        # insert_query = """
        #     INSERT IGNORE INTO AdSlots_VisionR
        #     (AdId, SubId, Start, End, StartTs, EndTs, Cut, Scenario, Duration, IsPub, Title, ReplacementAdId, Status, UpdateSent, PlaylistDate)
        #     VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
        # """

        # # Example format for PlaylistDate in your JSON: "2024-01-22"
        # date_format = "%Y-%m-%d"  # Adjust as necessary to match the format in your JSON

        # for playlist in ad_slots_json:  # Assuming `ad_slots_json` is the parsed JSON data
        #     playlist_date_str = playlist.get("playlistDate", "")

        #     # Validate and format playlist_date
        #     try:
        #         playlist_date = datetime.strptime(playlist_date_str, date_format).date()
        #     except ValueError:
        #         # If there's an error parsing the date, use the current date as default
        #         playlist_date = datetime.now().date()

        #     for ad in playlist.get('data', []):
        #         data_tuple = (
        #             ad.get('id', ''), 
        #             ad.get('subId', ''), 
        #             ad.get('start', 0), 
        #             ad.get('end', 0), 
        #             ad.get('startTs', 0), 
        #             ad.get('endTs', 0), 
        #             ad.get('cut', 0), 
        #             ad.get('scenario', ''),
        #             ad.get('duration', 0), 
        #             ad.get('isPub', 0), 
        #             ad.get('title', ''), 
        #             '', 
        #             'Pending', 
        #             False, 
        #             playlist_date
        #         )
        #         if ad.get('isPub', 0) == '1':
        #             cursor.execute(insert_query, data_tuple)
        #     # connection.commit()

        # cursor.close()

    # Main logic to fetch playlist data and insert into the database
    ad_slots = fetch_playlist_data(url)
     
    # print(ad_slots)
    if ad_slots:
        insert_ad_slots(ad_slots)
        print("Adslot Insert SuccessFully")
    else:
        print("Failed to fetch playlist data")

if __name__ == "__main__":
    process_ad_slots()