#!/usr/bin/env python
# coding: utf-8

# ### Import packages

# In[25]:


import unittest
from datetime import datetime, date, time
import csv


# In[26]:


class GlucoTechWebsite:
    def __init__(self):
        self.recentGlucose = None
        self.glucoseList = []
        self.classification = None
        self.dateTaken = None
        self.timeTaken = None
        
    def login(self, username, password):
        # Placeholder implementation
        pass
    
    def verifyUser(self, username, password):
        # Replace this with your actual authentication logic
        if username == "john.doe" and password == "password123":
            return True
        else:
            return False
        
    def displayRecentGlucose(self):
#This raises a ValueError if there are no glucose readings available, otherwise it returns a formatted string with the most recent glucose level.
        if self.recentGlucose is None:
            raise ValueError("No glucose readings available")
        else:
            return f"Most recent glucose reading: {self.recentGlucose} mg/dL"
    
    def classifyGlucoseLevel(self):
#sets the classification instance variable based on whether the recent glucose level is normal or abnormal, and creates an alert if the level is abnormal.
        if self.recentGlucose is None:
            print("No glucose readings available.")
        elif self.recentGlucose < 70 or self.recentGlucose > 200:
            self.classification = False
            print("Glucose level",f"{self.recentGlucose} is classified as abnormal.")
            self.createAlert()
            
        else:
            self.classification = True
            print("Glucose level",f"{self.recentGlucose} is classified as normal.")
            
    def createAlert(self):
        # Placeholder implementation
        pass
    
    def getDate(self):
#returns a formatted string with the date of the most recent glucose reading, or "No glucose readings available" if there are no readings available.
        if self.dateTaken is None:
            return "No glucose readings available"
        else:
            print(f"Date of most recent glucose reading: {self.dateTaken.strftime('%Y-%m-%d')}")
    
    def getTime(self):
#returns the time of the most recent glucose reading, or "None" if there are no readings available.
        if self.timeTaken is None:
            return "None"
        else:
            print(f"Time of most recent glucose reading: {self.timeTaken}")
            
    def writeFile(self, filename):
#takes a filename as an argument and writes the glucose data in the glucoseList instance variable to a CSV file with headers for date, time, and glucose level.
        with open(filename, mode='w', newline='') as glucose_file:
            glucose_writer = csv.writer(glucose_file)
            glucose_writer.writerow(['Date', 'Time', 'Glucose Level'])
            for glucose_data in self.glucoseList:
                glucose_writer.writerow([glucose_data[0], glucose_data[1], glucose_data[2]])

                
        
class TestGlucoTechWebsite(unittest.TestCase):
    "Test for GlucoTechWebsite"
    def setUp(self):
        self.gtw = GlucoTechWebsite()
        self.gtw.recentGlucose = 100 # Set a default recent glucose value for testing purposes
        self.phoneNumber = "555-555-5555"
        self.email = "example@example.com"
        self.gtw.dateTaken = datetime.now().date()
        self.gtw.timeTaken = datetime.now().time()
        
    def test_verifyUser(self):
        self.assertFalse(self.gtw.verifyUser("jane.doe", "password456"))
        self.assertTrue(self.gtw.verifyUser("john.doe", "password123"))
    
    def test_displayRecentGlucose(self):
        self.assertEqual(self.gtw.recentGlucose, 100)
        recent_glucose_str = self.gtw.displayRecentGlucose()
        self.assertEqual(self.gtw.displayRecentGlucose(), "Most recent glucose reading: 100 mg/dL")
        print(recent_glucose_str)
        
        self.gtw.recentGlucose = None
        with self.assertRaises(ValueError):
            self.gtw.displayRecentGlucose()
    
    def test_classifyGlucoseLevel(self):
        self.gtw.recentGlucose = 50
        self.gtw.classifyGlucoseLevel()
        self.assertFalse(self.gtw.classification)
    
        self.gtw.recentGlucose = 250
        self.gtw.classifyGlucoseLevel()
        self.assertFalse(self.gtw.classification)
        
        self.gtw.recentGlucose = 150
        self.gtw.classifyGlucoseLevel()
        self.assertTrue(self.gtw.classification)
        
    def test_getDate(self):
        self.assertIsNone(self.gtw.getDate())
        self.gtw.dateTaken = date.today()
        
    def test_getTime(self):       
        self.assertEqual(self.gtw.getTime(), None)
        
        self.gtw.timeTaken = datetime.now().time()
        self.assertIsNotNone(self.gtw.timeTaken)
        self.assertIsInstance(self.gtw.timeTaken, time)
        
    def test_writeFile(self): 
        self.gtw.glucoseList = [("1/1/23", "10:00", '100'), ("1/2/23", "11:00", '120'),['1/3/23', "15:00", '200']]
        self.gtw.writeFile("diabetes..csv")

        with open("diabetes..csv", mode="r") as csv_file:
            csv_reader = csv.reader(csv_file)
            header_row = next(csv_reader)
            self.assertEqual(header_row, ['Date', 'Time', 'Glucose Level'])

            rows = [row for row in csv_reader]
            expected_output = [("1/1/23", "10:00", '100'), ("1/2/23", "11:00", '120'),['1/3/23', "15:00", '200']]
            self.assertEqual(rows, [['1/1/23', "10:00", '100'], ['1/2/23', "11:00", '120'],['1/3/23', "15:00", '200']])
            
        for row, expected_row in zip(rows, expected_output):
            self.assertEqual(f"{row[0]},{row[1]},{row[2]}", ",".join(expected_row))
            print(row)
  
if __name__ == '__main__':
    unittest.main(argv=['first-arg-is-ignored'], exit=False)


# In[ ]:




