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

from troggle.core.models.logbooks import LogbookEntry
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):
    # see test_logins.py for the tests to check that logged-in users work
    fixtures = ["auth_users"] # contains user 'expotest' with a hash => password = 'secretword'
    
    @classmethod
    def setUpTestData(cls):
        def make_person(firstname, lastname, nickname=False, vfho=False):
            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": cls.test_expo}
            otherAttribs = {}
            pe = PersonExpedition.objects.create(**otherAttribs, **coUniqueAttribs)
            
            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
        
        coUniqueAttribs = {"year": TEST_YEAR}
        otherAttribs = {"name": f"CUCC expo-test {TEST_YEAR}"}
        cls.test_expo = Expedition.objects.create(**otherAttribs, **coUniqueAttribs)
        
        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):
        from django.contrib.auth.models import User

        self.user = User.objects.get(username="expotest") # has password 'secretword' from fixture
        self.client = Client()


    def tearDown(self):
        pass

    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 Logbook Entry on 1986-07-31",
            "Other names \(comma separated\)", # regex 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_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_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 + "'")