import os
import unittest
import logging
from cmath import isclose
from decimal import Decimal
from unittest import TestCase

import pandas as pd
from pandas import DataFrame
from supplychainpy import model_inventory
from supplychainpy._helpers._config_file_paths import ABS_FILE_PATH_APPLICATION_CONFIG
from supplychainpy._helpers._pickle_config import deserialise_config
from supplychainpy.model_inventory import analyse, recommendations
from supplychainpy.sample_data.config import ABS_FILE_PATH
# logging.basicConfig(filename='suchpy_tests_log.txt', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

class TestBuildModel(TestCase):
    _yearly_demand = {'jan': 75, 'feb': 75, 'mar': 75, 'apr': 75, 'may': 75, 'jun': 75, 'jul': 25,
                      'aug': 25, 'sep': 25, 'oct': 25, 'nov': 25, 'dec': 25}
    _yearly_demand2 = {'jan': 75}

    _categories = ('unit_cost', 'sku', 'reorder_level', 'safety_stock', 'reorder_quantity', 'ABC_XYZ_Classification',
                   'revenue', 'standard_deviation', 'quantity_on_hand', 'average_orders', 'shortages', 'excess_stock',
                   'demand_variability')

    _expected_values = ('50', 'RX983-90', '99', '12', '57', '', '360000', '25', '390', '50', '0', '205', '0.500')

    def test_model_orders_type(self):
        """Tests analyse_orders returns a 'dict' type. """
        summary = model_inventory.analyse_orders(self._yearly_demand,
                                                 sku_id='RX983-90',
                                                 lead_time=Decimal(3),
                                                 unit_cost=Decimal(50.99),
                                                 reorder_cost=Decimal(400),
                                                 z_value=Decimal(1.28),
                                                 retail_price=Decimal(600),
                                                 quantity_on_hand=Decimal(390))

        self.assertIsInstance(summary, dict)

    def test_model_orders_length(self):
        """supplied orders data must be greater than 3 items long."""
        with self.assertRaises(expected_exception=ValueError):
            summary = model_inventory.analyse_orders(self._yearly_demand2,
                                                     sku_id="RX983-90",
                                                     lead_time=Decimal(3),
                                                     unit_cost=Decimal(50.99),
                                                     reorder_cost=Decimal(400),
                                                     z_value=Decimal(.28),
                                                     retail_price=Decimal(600),
                                                     quantity_on_hand=Decimal(390))

    def test_model_orders_content(self):
        """ test the return values for simple analyse orders"""
        summary = model_inventory.analyse_orders(self._yearly_demand,
                                                 sku_id="RX983-90",
                                                 lead_time=Decimal(3),
                                                 unit_cost=Decimal(50),
                                                 reorder_cost=Decimal(400),
                                                 z_value=Decimal(.28),
                                                 retail_price=Decimal(600),
                                                 quantity_on_hand=Decimal(390))

        for i, k in zip(self._categories, self._expected_values):
            self.assertEqual(str(k), summary.get(i))

            # finish with all members

    def test_standard_deviation_row_count(self):
        d = model_inventory.analyse(file_path=ABS_FILE_PATH['COMPLETE_CSV_SM'],
                                    z_value=Decimal(1.28),
                                    reorder_cost=Decimal(400),
                                    retail_price=Decimal(455),
                                    file_type='csv')

        analysed_orders = [demand.orders_summary() for demand in d]

        self.assertEqual(len(d), 39)

    def test_file_path_extension_row(self):