from zope.sqlalchemy import ZopeTransactionExtension

from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy.orm import scoped_session
from sqlalchemy.orm import sessionmaker

DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension(), expire_on_commit=False))
Base = declarative_base()


def initialize_sql(engine):
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine
    if not schema_exists(engine):
        fixtures(engine)


def schema_exists(engine):
    from brewman.models.master import DbSetting
    with engine.connect() as connection:
        return engine.dialect.has_table(connection, DbSetting.__tablename__)


def fixtures(engine):
    import transaction
    import uuid
    from brewman.models.messaging import Tag, Thread, Subscriber, thread_tag, Post
    from brewman.models.voucher import Attendance, Batch, Fingerprint, Inventory, Journal, Product, SalaryDeduction, Voucher, VoucherType
    from brewman.models.master import Product, AttendanceType, CostCenter, Employee, Ledger, LedgerBase, LedgerType, ProductGroup
    from brewman.models.auth import Client, Group, Role, User, role_group, user_group

    Base.metadata.create_all(engine)

    user = User('Admin', '123456', False, uuid.UUID('8de98592-76d9-c74d-bb3f-d6184d388b5a'))
    DBSession.add(user)

    groups = [Group('Owner', uuid.UUID('52e08c0c-048a-784f-be10-6e129ad4b5d4')),
              Group('Accountant', uuid.UUID('bc4c2d23-437a-984d-abd4-7d5fce677547')),
              Group('Accounts Manager', uuid.UUID('cfc44fa7-3392-5b45-b311-5959333f568f'))]

    for group in groups:
        DBSession.add(group)
        user.groups.append(group)

    roles = [Role('Attendance', uuid.UUID('09d05434-a09a-fa45-963b-769a2e3fc667')),
             Role('Trial Balance', uuid.UUID('3b099fec-ddc5-4243-b30e-afb78d9ca14a')),
             Role('Cash Flow', uuid.UUID('c4d3ae29-420b-ea4c-ae90-00a356263fd9')),
             Role('Cost Centers', uuid.UUID('6fcc1a20-6aec-e840-b334-1632b34aeab8')),
             Role('Users', uuid.UUID('c5b7d9d7-f178-0e45-8ea4-bf4e08ec901b')),
             Role('Daybook', uuid.UUID('c3edb554-a057-8942-8030-37b8e926d583')),
             Role('Edit Posted Vouchers', uuid.UUID('d6675817-ddf5-bf40-9de6-fa223eb4aaa6')),
             Role('Employees', uuid.UUID('e4edd0ac-7f5d-e64d-8611-73fdc4cd8ba2')),
             Role('Fingerprints', uuid.UUID('d9c45323-f997-ba46-9407-8a7145f0828b')),
             Role('Issue', uuid.UUID('03b602eb-f58a-b94f-af58-8cb47d7849d0')),
             Role('Journal', uuid.UUID('7661388f-62ce-1c41-8e0d-0326ee5d4018')),
             Role('Accounts', uuid.UUID('f438262f-72dd-2f4e-9186-5abc3af44fba')),
             Role('Product Ledger', uuid.UUID('018a2408-e804-1446-90c5-b015829da6ba')),
             Role('Backdated Vouchers', uuid.UUID('b67b2062-5ca7-134f-8258-5d284dd92426')),
             Role('Payment', uuid.UUID('f85c0b52-c3fd-7141-8957-7a56cdc014a4')),
             Role('Post Vouchers', uuid.UUID('36e741da-1a57-b047-a59e-dcd58fcf4338')),
             Role('Products', uuid.UUID('74fa6d21-eebb-e14c-8153-bebc57190ab4')),
             Role('Product Groups', uuid.UUID('08413a22-cf88-fd43-b2b7-365d2951d99f')),
             Role('Profit & Loss', uuid.UUID('0492ebb3-76f3-204e-ab94-bbfe880f0691')),
             Role('Purchase', uuid.UUID('12335acb-8630-2d41-a191-1517c8d172de')),
             Role('Purchase Entries', uuid.UUID('78a6422b-aa11-174c-9dfa-412a99e87e02')),
             Role('Purchase Return', uuid.UUID('ab33196e-d9e4-114c-ac8c-997954363756')),
             Role('Receipt', uuid.UUID('1f1ce53e-76ff-a346-974a-65db6f606e5f')),
             Role('Closing Stock', uuid.UUID('97515732-24e4-c94d-9585-d4bd7f6c7891')),
             Role('Ledger', uuid.UUID('a2120944-243f-3f49-be57-0ad633ce4801')),
             Role('Raw Material Cost', uuid.UUID('d462842b-baf1-2343-95e5-ffdba9bbc163')),
             Role('Edit Other User\'s Vouchers', uuid.UUID('a8328891-7ce2-a943-8c29-2eabc1ffeea3')),
             Role('Clients', uuid.UUID('cfad44f0-f2a9-7045-89d7-9019cf0f371a')),
             Role('Salary Deduction', uuid.UUID('92d70e80-1c32-384d-959e-abf84b804696')),
             Role('Messages', uuid.UUID('f586d128-b6d9-4090-a913-78fcbdb68e59')),
             Role('Lock Date', uuid.UUID('d52de0be-9388-4b0b-a359-7e122ab6e53a')),
             Role('Net Transactions', uuid.UUID('2c40f7cf-67fc-4efa-a670-8d16a2e7884d')),
             Role('Balance Sheet', uuid.UUID('40deb018-b8f2-460a-88be-8972c9fcdf04')),
             Role('Advanced Delete', uuid.UUID('197ebcd2-bc4a-4b65-a138-ce942ece32ea')),
             Role('Rebase', uuid.UUID('de204a88-5f9d-4579-a2d6-aa2f25efde42')),
             Role('Reset Stock', uuid.UUID('aecaf82f-aa41-4634-b754-0c1308b621b1')),
             Role('Reconcile', uuid.UUID('a5cb51cb-e38e-4705-84a7-cc1e9a8b866b')),
             Role('Stock Movement', uuid.UUID('20b707ee-2b59-41ad-be87-76d5fe1efca8')),
             Role('Purchases', uuid.UUID('cf7019c8-3fd3-45b0-9a42-601029ce5b71')),
             Role('Dashboard', uuid.UUID('53eecc09-bd06-4890-b6f5-6885dda762d4'))]

    for role in roles:
        DBSession.add(role)
        groups[0].roles.append(role)

    cost_centers = [CostCenter('Overall', uuid.UUID('36f59436-522a-0746-ae94-e0f746bf6c0d'), True),
                    CostCenter('Purchase', uuid.UUID('7b845f95-dfef-fa4a-897c-f0baf15284a3'), True),
                    CostCenter('Kitchen', uuid.UUID('b2d398ce-e3cc-c542-9feb-5d7783e899df'), True)]

    for cost_center in cost_centers:
        DBSession.add(cost_center)

    ledgers = [Ledger(1, 'All Purchases', 2, True, False, uuid.UUID('7b845f95-dfef-fa4a-897c-f0baf15284a3'),
                      uuid.UUID('240dd899-c413-854c-a7eb-67a29d154490'), True),
               Ledger(1, 'Local Purchase', 9, True, False, uuid.UUID('36f59436-522a-0746-ae94-e0f746bf6c0d'),
                      uuid.UUID('d2b75912-505f-2548-9093-466dfff6a0f9'), True),
               Ledger(1, 'ESI/PF - Payable', 11, True, False, uuid.UUID('36f59436-522a-0746-ae94-e0f746bf6c0d'),
                      uuid.UUID('42277912-cc18-854b-b134-9f4b00dba419'), True),
               Ledger(2, 'ESI/PF - Expense', 7, True, False, uuid.UUID('36f59436-522a-0746-ae94-e0f746bf6c0d'),
                      uuid.UUID('d2a1a286-e900-764b-a1a5-9f4b00dbb940'), True),
               Ledger(1, 'Cash in Hand', 1, True, False, uuid.UUID('36f59436-522a-0746-ae94-e0f746bf6c0d'),
                      uuid.UUID('ed2341bb-80b8-9649-90db-f9aaca183bb3'), True),
               Ledger(1, 'Staff Salary', 7, True, False, uuid.UUID('36f59436-522a-0746-ae94-e0f746bf6c0d'),
                      uuid.UUID('5c2b54d0-c174-004d-a0d5-92cdaadcefa7'), True),
               Ledger(1, 'Suspense', 4, True, False, uuid.UUID('36f59436-522a-0746-ae94-e0f746bf6c0d'),
                      uuid.UUID('3854e317-6f3b-5142-ab26-9c44d4cddd08'), True)]

    for ledger in ledgers:
        DBSession.add(ledger)

    product_group = ProductGroup('Suspense', uuid.UUID('ae59a20c-87bb-444a-8abb-915ad5e58b83'), True)
    DBSession.add(product_group)

    product = Product(1, 'Suspense', '', 1, '', 1, False, uuid.UUID('ae59a20c-87bb-444a-8abb-915ad5e58b83'),
                      uuid.UUID('240dd899-c413-854c-a7eb-67a29d154490'), 0, True,
                      uuid.UUID('aa79a643-9ddc-4790-ac7f-a41f9efb4c15'), True)
    DBSession.add(product)

    transaction.commit()

