import os, sys
from datetime import datetime
from dateutil import relativedelta

import django
sys.path.append('../../../')
os.environ['DJANGO_SETTINGS_MODULE'] = 'ds_events.settings'
django.setup()

from time import sleep
from pprint import pprint as ppr
import pytz

from events.models import Organization, Scraper
import events.digitools as digitools

count = 0

venue, created = Organization.objects.get_or_create(
     name="Dakota",
     city="Minneapolis",
     website="https://www.dakotacooks.com/events/",
     is_venue=True
    )

scraper,item_count_start = digitools.getScraper(venue)

event_type = ""

# Time Signatures
tz = pytz.timezone("US/Central")
DATETIME_FORMAT = '%a %b %d, %Y • %I:%M%p'
DATETIME_FORMAT_2 = '%a %b %d, %Y • %I%p'

def get_events(ps, event_type):
    links = ps.xpath('.//*/div[@class="wicked-event-title tooltipstered"]/a/@href')
    links = set(links)
    for l in links:
        pse = getSource(br, l)
        event = {}
        event['scraper'] = scraper
        event['calendar'] = scraper.calendar
        event['link'] = l
        try:
            event['time'] = pse.xpath('.//*/span[@class="text-uppercase"]/text()')[0].strip()
            event['dateStamp'] = datetime.strptime(event['time'], DATETIME_FORMAT) 
            event['title'] = pse.xpath('.//*/div[@class="sidebar-group"]/h1/text()')[0]
            # event['detail-headers'] = pse.xpath('.//*/ul[@class="eo-event-meta"]/li/strong/text()')
            # event['details'] = pse.xpath('.//*/ul[@class="eo-event-meta"]/li/text()')

        except:
            try:
                event['title'] = pse.xpath('.//*/div[@class="sidebar-group"]/h1/text()')[0]
                event['dateStamp'] = datetime.strptime(event['time'], DATETIME_FORMAT_2)
                event['time'] = pse.xpath('.//*/span[@class="text-uppercase"]/text()')[0]
            except Exception as e:
                print(e)
                print("failed event: ", event)
        ppr(event)
        try:
            digitools.createBasicEvent(event, "Mu", venue)
            scraper.items+=1
        except Exception as e:
            print(e)
            print('failed to create: ', event)

if len(sys.argv) >= 2:
    arg1 = sys.argv[1]
    br = digitools.getBrowser(arg1)
else:
    print("No run_env")
    quit()


ps = digitools.getSource(br, venue.website + '?wicked_month=04&wicked_year=2025&wicked_view=month')
get_events(ps, "Mu")
sleep(1)


digitools.updateScraper(scraper, item_count_start)

br.close()