# from api.store import Store, factory
import api.store as store
import unittest
import json
import stripe

class TestStore(unittest.TestCase):
    def setUp(self):
        stripe.api_key = 'sk_test_I16uOM1ZfGALSc03AF0xqN2z'
    def test_getPlans(self):
        
        
        mystore = store.factory.instance(name='music')
        self.assertTrue(mystore.get.plans())
        
    def test_getProducts(self):
        mystore = store.factory.instance(name='smart-top')
        self.assertTrue(mystore.get.products())
    def test_getUser(self):
        """
        """
        email='nyemba@gmail.com'
        mystore = store.factory.instance(name='music',email=email)
        # mystore.init('nyemba@gmail.com')
        # self.assertTrue(mystore.get.user.info())
        user = mystore.user 
        user.init(email)
        
        self.assertTrue(user.info())
    def test_UserPlan(self):
        """
        For a given product we would like to know what our user's plan is 
        """
        email = 'nyemba@gmail.com'
        mystore = store.factory.instance(name='music',email=email)
        user = mystore.user 
        user.init (email)
        user.plan.info()  
        
        
    def _test_CancelUserPlan(self) :
        email = 'nyemba@gmail.com'
        mystore = store.factory.instance(name='music',email=email)
        user = mystore.user
        # mystore.init(email)        
        # plan = mystore.get.user.plan()
        user.init(email)
        plan = user.plan.info()
        
        self.assertTrue ('id' in plan)
        mystore.plan.cancel(id=plan['id'])
        self.assertTrue ('id' in plan)

        # pass
        pass
    def test_SubscribeToPlan(self):
        email = 'nyemba@gmail.com'
        mystore = store.factory.instance(name='music',email=email)
        user    = mystore.user
        user.init(email)
        plans = mystore.get.plans()
        uplan = user.plan.info()
        # uplan = mystore.get.user.plan()
        ids     =  [plan['id'] for plan in plans]
        if not uplan :
            id  = [item for item in plans if item['amount'] == 0]
            id = id[0]['id']
            mystore.plan.subscribe(id=id,email=email)
            pass
        else:
            
            new_id  = list(set(ids) - set( [uplan['id']] ))[0]
            mystore.plan.upgrade(new_id,email)
        # mystore.plan.subscribe(new_id,email)
        pass
if __name__ == '__main__':
    unittest.main()