# -*- coding: utf-8 -*-
from datetime import datetime, timedelta, date
from mock import Mock
from os import path

from django.test.testcases import TestCase

from freezegun.api import freeze_time

from djanalytics import models
from djanalytics.management.commands.collect_reporting_stats import Command


class TestCollectReportingStatsCommand(TestCase):

    def setUp(self):
        self.dja_client = models.Client.objects.create(
            name='testclient'
        )
        self.dja_property = models.WebProperty.objects.create(
            name='Djanalytics',
            slug='djanalytics',
        )
        self.dja_domain = models.Domain.objects.create(
            name='djanalytics.example.com',
            client=self.dja_client,
            web_property=self.dja_property,
        )
        funnel_type, created = models.PageType.objects.get_or_create(
            code=models.PageType.FUNNEL
        )
        models.PagePattern.objects.create(
            name='Funnel',
            code='funnel',
            page_type=funnel_type,
            pattern='/page_two',
            display_path='/page_two',
            client=self.dja_client,
        )
        models.Page.objects.create(
            path='/page_two',
            page_type=funnel_type,
            client=self.dja_client
        )
        checkout_type, created = models.PageType.objects.get_or_create(
            code=models.PageType.CONVERSION,
            defaults={
                'name': 'Checkout'
            }
        )
        models.PagePattern.objects.create(
            name='Conversion',
            code='conversion',
            page_type=checkout_type,
            pattern='/page_three/(\d+)',
            display_path='/page_three',
            client=self.dja_client,
        )
        self.page = models.Page.objects.create(
            path='/page_three/123/',
            page_type=checkout_type,
            client=self.dja_client,
        )

    def test_collect(self):
        tracking_key = models.generate_uuid()
        tracking_user_id = models.generate_uuid()
        right_now = datetime.now()
        for cnt, page in enumerate(['/page_three/123/', '/page_two', '/page_one']):
            re = models.RequestEvent.objects.create(
                ip_address='127.0.0.1',
                user_agent='Mozilla/5.0 (Windows NT 5.1; rv:30.0) Gecko/20100101 Firefox/30.0',
                tracking_key=tracking_key,
                tracking_user_id=tracking_user_id,
                protocol='http',
                domain='djanalytics.example.com',
                path=page,
                query_string='',
                method='GET',
                client=self.dja_client,
            )
            right_now = right_now - timedelta(seconds=(cnt*2))
            re.created=right_now
            re.save()
        command = Command()
        command.stdout = Mock()
        command.handle()
        page_visits = models.PageVisit.objects.order_by('request_event__created')
        self.assertEqual(4, page_visits[0].duration)
        self.assertEqual(2, page_visits[1].duration)
        self.assertEqual(None, page_visits[2].duration)
        visit = models.Visit.objects.get()
        self.assertEqual(6, visit.duration)
        self.assertEqual(1, visit.conversion_count)
        self.assertEqual(visit.last_page.path, '/page_three/123/')
        self.assertEqual(visit.first_page.path, '/page_one')
  
    def test_collect_no_funnel(self):
        # a funnel page MUST precede a conversion page
        # this prevents "order confirmation" type pages from being
        # counted as conversions
        tracking_key = models.generate_uuid()
        tracking_user_id = models.generate_uuid()
        right_now = datetime.now()
        for cnt, page in enumerate(['/page_three/123/', '/page_one']):
            re = models.RequestEvent.objects.create(
                ip_address='127.0.0.1',