from django.core.management.base import BaseCommand, CommandError
from catalog.models import User, News, Publisher, Topic, audioTime, Search
from django.db.models import Q
from django.core.paginator import Paginator
from django.contrib.postgres.search import SearchQuery

import os
import json
import csv
from django.core import serializers

import sys
import datetime

class Command(BaseCommand):
	help = 'Custom search'


	def add_arguments(self, parser):
		parser.add_argument('jsonfile', nargs=1, type=str) #(options['basedir'][0]
		parser.add_argument('outfile', nargs=1, type=str)

	def handle(self, *args, **options):
		filename = options['jsonfile'][0]
		with open(filename, 'r') as f:
			searchCfg = json.load(f)
		myQuery = ~Q()
		myQuery &= Q(publisher_id__in=searchCfg['publishers'])
		myQuery &= Q(date__gte=searchCfg['startDate'])
		myQuery &= Q(date__lte=searchCfg['endDate'])
		
		qText = None
		for i in searchCfg["phrases"]:
			if qText is None:
				qText = SearchQuery(i, search_type='phrase')
			else:
				qText |= SearchQuery(i, search_type='phrase')


		#myQuery &=qText

		news = News.objects.filter(myQuery, search_vector=qText)
		print(news.count())
		if news.count() >0:
			nn = news[0]
			print(nn.publisher.shortName, nn)
			qs_json = list() #serializers.serialize('json', news)
			for n in news:
				qs_json.append({"publisher":n.publisher.shortName, "date":n.date, "title":n.title, "text":n.text, "url":n.url})
			print(qs_json[0])

			with open(options['outfile'][0], "w") as ff:

				w = csv.DictWriter(ff, qs_json[0].keys())
				w.writeheader()
				for i in qs_json:
					w.writerow(i)
