"""
We are using unittest for troggle.

Note that the database has not been parsed from the source files when these tests are run,
so any path that relies on data being in the database will fail.

The simple redirections to files which exist, e.g. in
/expoweb/
/photos/
etc. will test fine.

But paths like this:
/survey_scans/
/caves/
which rely on database resolution will fail unless a fixture has been set up for
them.

https://docs.djangoproject.com/en/dev/topics/testing/tools/
"""
import re
import subprocess
import unittest
from http import HTTPStatus

from django.contrib.auth.models import User
from django.test import Client, SimpleTestCase, TestCase

import troggle.parsers.logbooks as lbp
from troggle.core.models.logbooks import LogbookEntry
from troggle.core.models.troggle import DataIssue, Expedition, Person, PersonExpedition
from troggle.core.utils import current_expo

current_year = current_expo()


def create_user(name=None, last_name="Caver", is_superuser=False):
    u = User()
    u.username = name
    u.email = f"philip.sargent+{name}@gmail.com"
    u.first_name, u.last_name = name, last_name
    u.set_password("secretword") # all test users have same password
    u.save()
    return u

def create_person(firstname, lastname, nickname=False, vfho=False, exped=None):
    fullname = f"{firstname} {lastname}"
    slug=f"{firstname.lower()}-{lastname.lower()}"
    coUniqueAttribs = {"first_name": firstname, "last_name": (lastname or ""), "slug": slug,}
    otherAttribs = {"is_vfho": vfho, "fullname": fullname, "nickname": nickname}
    person = Person.objects.create(**otherAttribs, **coUniqueAttribs)
    
    coUniqueAttribs = {"person": person, "expedition": exped}
    otherAttribs = {}
    pe = PersonExpedition.objects.create(**otherAttribs, **coUniqueAttribs)
    return person
    
TEST_YEAR = "1986"
lbp.ENTRIES[TEST_YEAR] = 4 # number of entries in the test logbook 
    
class ImportTest(TestCase):
    # see test_logins.py for the tests to check that logged-in users work
    
    @classmethod
    def setUpTestData(cls):            
        import troggle.settings as settings
        
        LOGBOOKS_PATH = settings.EXPOWEB / lbp.LOGBOOKS_DIR 
        
        cls.test_logbook = LOGBOOKS_PATH / TEST_YEAR / lbp.DEFAULT_LOGBOOK_FILE
        frontmatter_file = LOGBOOKS_PATH / TEST_YEAR / "frontmatter.html"
        if frontmatter_file.is_file():
            frontmatter_file.unlink() # delete if it exists
        
        coUniqueAttribs = {"year": TEST_YEAR}
        otherAttribs = {"name": f"CUCC expo-test {TEST_YEAR}"}
        cls.test_expo = Expedition.objects.create(**otherAttribs, **coUniqueAttribs)
        
        fred = create_person("Fred", "Smartarse", nickname="freddy", exped=cls.test_expo)
        phil = create_person("Phil", "Tosser", nickname="tosspot", exped=cls.test_expo)
        dave = create_person("David", "Smartarse", "", exped=cls.test_expo)
        mike = create_person("Michael", "Wideboy", "WB", vfho=True, exped=cls.test_expo)
        # NOT created Kurt, as the whole point is that he is a guest.
         
    def setUp(self):
        create_user(name="expo") # needed for current_year() 
        self.user = create_user(name="expotest") 
        self.client = Client()


    def tearDown(self):
        User.objects.all().delete()
        Person.objects.all().delete()
        PersonExpedition.objects.all().delete()
        Expedition.objects.all().delete()

    def test_logbook_exists(self):
        self.assertTrue(self.test_logbook.is_file())
        
    def test_logbook_parse_issues(self):
        """This is just testing the db not the web page
        """
        lbp.LoadLogbook(self.test_expo) # i.e. load the 1986 logbook
        
        issues = DataIssue.objects.all()
        messages = []
        for i in issues:
            if i.parser=="logbooks":
                # f"{self.parser} - {self.message}"
                messages.append(i.message)
                print(f"'{i.message}'")
                
        expected = [
            "! - 1986 No name match for: 'Kurt Keinnamen' in entry",
        ]
        
        not_expected = [
            " ! - 1986 EXCEPTION:: 'Dave Smartarse' (Dave Smartarse) in entry tid='1986-07-27a' for this year.",
            " ! - 1986 Warning: logentry: surface  - stupour - no expo member author for entry '1986-07-31a'",
            " ! - 1986 Warning: logentry: 123  - wave 2 - no expo member author for entry '1986-08-01a'",
        ]
        # with open('_test_response.txt', 'w') as f:
            # for m in messages:
                # f.write(m)  
        messages_text = ", ".join(messages)
        for e in expected:
            phmatch = re.search(e, messages_text)
            self.assertIsNotNone(phmatch, f"Failed to find expected text: '{e}' in\n{messages_text}")
        for e in not_expected:
            phmatch = re.search(e, messages_text)
            self.assertIsNone(phmatch, f"Found unexpected text: '{e}' in\n{messages_text}")
            
    def test_lbe(self):
        lbp.LoadLogbook(self.test_expo) # i.e. load the 1986 logbook, which has this logbook entry
        
        response = self.client.get(f"/logbookentry/1986-07-27/1986-07-27a")
        self.assertEqual(response.status_code, HTTPStatus.OK)
        content = response.content.decode()
        # with open('_test_response_1986-07-27a.html', 'w') as f:
            # f.write(content)        
        expected = [
            "<title>Logbook CUCC expo-test 1986 123  - 123 Wave 1</title>",
            "Smartarse rig first section of new pitches. Second wave arrives and takes over rigging.",
        ]
        for ph in expected:
            phmatch = re.search(ph, content)
            self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'")

    def test_lbe_new(self):
        """This page requires the user to be logged in first, hence the extra shenanigans
        """
        c = self.client
        u = self.user
        c.login(username=u.username, password="secretword")

        response = self.client.get(f"/logbookedit/")
        self.assertEqual(response.status_code, HTTPStatus.OK)
        content = response.content.decode()
        # with open('_test_response.html', 'w') as f:
            # f.write(content) 
        expected = [
            "New Logbook Entry in ",
            "Everyone else involved",
            "Place: cave name, or 'plateau', 'topcamp' etc.",
        ]
        for ph in expected:
            phmatch = re.search(ph, content)
            self.assertIsNotNone(phmatch, f"({response.status_code}) Failed to find expected text: '" + ph + "'")
            

    def test_lbe_edit(self):
        """This page requires the user to be logged in first, hence the extra shenanigans
        """
        c = self.client
        u = self.user
        c.login(username=u.username, password="secretword")
 
        lbp.LoadLogbook(self.test_expo) # i.e. load the 1986 logbook, which has this logbook entry
        # muliple loads are overwriting the lbes and incrementing the a, b, c etc, so get one that works
        lbe = LogbookEntry.objects.get(date="1986-07-31") # only one on this date in fixture
        
        response = self.client.get(f"/logbookedit/{lbe.slug}") 
        self.assertEqual(response.status_code, HTTPStatus.OK)
        content = response.content.decode()
        # with open('_test_response_edit.html', 'w') as f:
            # f.write(content)        
        expected = [
            "Edit Existing Logbook Entry on 1986-07-31",
            r"Other names \(comma separated\)", # regex match so slashes need to be espcaped
            "Place: cave name, or 'plateau', 'topcamp' etc.",
        ]
        for ph in expected:
            phmatch = re.search(ph, content)
            self.assertIsNotNone(phmatch, f"({response.status_code}) Failed to find expected text: '" + ph + "'")
            
    def test_aliases(self):
        # FIX THIS
        # Problem: '' empty string appears as valid alias for David Smartarse
        response = self.client.get(f"/aliases/{TEST_YEAR}")
        self.assertEqual(response.status_code, HTTPStatus.OK)
        content = response.content.decode()
        # with open('_test_responsealiases.html', 'w') as f:
            # f.write(content)        
        ph = f"'fsmartarse'"
        phmatch = re.search(ph, content)
        self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'")
 
    def test_survexfiles(self):
        # Needs another test with test data
        response = self.client.get("/survexfile/caves")
        self.assertEqual(response.status_code, HTTPStatus.OK)
        content = response.content.decode()
        # with open('_test_response.html', 'w') as f:
            # f.write(content)        
        ph = f"Caves with subdirectories"
        phmatch = re.search(ph, content)
        self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'")
            
    def test_people(self):
        # Needs another test with test data
        response = self.client.get("/people")
        self.assertEqual(response.status_code, HTTPStatus.OK)
        content = response.content.decode()
        # with open('_test_response.html', 'w') as f:
            # f.write(content)        
        ph = f"<td><a href=\"/personexpedition/fred-smartarse/{TEST_YEAR}\">{TEST_YEAR}</a></td>"
        phmatch = re.search(ph, content)
        self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'")
                        