"""
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/
/expofiles/
/expofiles/documents/
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.test import Client, SimpleTestCase, TestCase

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

TEST_YEAR = "1986"
lbp.ENTRIES[TEST_YEAR] = 4 # number of entries in the test logbook 
    
class ImportTest(TestCase):
    
    @classmethod
    def setUpTestData(cls):
        def make_person(firstname, lastname, nickname=False, vfho=False, guest=False):
            fullname = f"{firstname} {lastname}"
            lookupAttribs = {"first_name": firstname, "last_name": (lastname or "")}
            nonLookupAttribs = {"is_vfho": vfho, "fullname": fullname, "nickname": nickname}
            person = Person.objects.create(**nonLookupAttribs, **lookupAttribs)
            
            lookupAttribs = {"person": person, "expedition": cls.test_expo}
            nonLookupAttribs = {"is_guest": guest}
            pe = PersonExpedition.objects.create(**nonLookupAttribs, **lookupAttribs)
            
            return person
            
        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
        
        lookupAttribs = {"year": TEST_YEAR}
        nonLookupAttribs = {"name": f"CUCC expo-test {TEST_YEAR}"}
        cls.test_expo = Expedition.objects.create(**nonLookupAttribs, **lookupAttribs)
        
        fred = make_person("Fred", "Smartarse", nickname="freddy")
        phil = make_person("Phil", "Tosser", nickname="tosspot")
        dave = make_person("David", "Smartarse", "")
        mike = make_person("Michael", "Wideboy", "WB", vfho=True)
        # NOT created Kurt, as the whole point is that he is a guest.
 
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_logbook_exists(self):
        self.assertTrue(self.test_logbook.is_file())
        
    def test_logbook_parse(self):
        
        lbp.LoadLogbook(self.test_expo)        
        
        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 tid='1986_s02' for this expedition year.",
        ]
        
        not_expected = [
            " ! - 1986 No name match for: 'Dave Smartarse' in entry tid='1986_s01' for this expedition year.",
            " ! - 1986 Warning: logentry: surface  - stupour - no expo member author for entry '1986_s03'",
            " ! - 1986 Warning: logentry: 123  - wave 2 - no expo member author for entry '1986_s02'",
        ]
        
        for e in expected:
            self.assertIn(e, messages)
        for e in not_expected:
            self.assertNotIn(e, messages)
            
    def test_aliases(self):
        # Needs another test with test data
        response = self.client.get(f"/aliases/{TEST_YEAR}")
        self.assertEqual(response.status_code, HTTPStatus.OK)
        content = response.content.decode()
        with open('_test_response.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_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/FredSmartarse/{TEST_YEAR}\">{TEST_YEAR}</a></td>"
        phmatch = re.search(ph, content)
        self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'")
                        