import logging
import datetime
import textwrap

import mango


logging.basicConfig()

client = mango.Client()
db = client.test_db


@mango.assign(db)
class Employee(mango.Document):
    def __init__(self,
                 name = 'John Doe',
                 pay = 0,
                 hired_on = None,
                 **kwargs
                 ):

        if hired_on is None:
            hired_on = datetime.datetime.today()

        super().__init__(
                name = name,
                pay = pay,
                hired_on = hired_on,
                **kwargs,
        )


@mango.assign(db, collection = Employee.collection)
class Manager(Employee):
    def __init__(self,
                 employee_names = (),
                 **kwargs
                 ):
        super().__init__(
                employee_names = employee_names,
                **kwargs
        )


db2 = client.foo

try:
    @mango.assign(db2, collection = Employee.collection)
    class Post(mango.Document):
        pass
except mango.MangoException as e:
    print(e)


db.drop_collection(Employee.collection)

alice = Employee(
        name = 'Alice',
        pay = 10000,
        hired_on = datetime.datetime(1992, 5, 30),
)

bob = Employee(
        name = 'Bob',
        pay = 10000,
        hired_on = datetime.datetime.today(),
)

lenny = Manager(
        name = 'Lenny',
        pay = 15000,
        hired_on = datetime.datetime(1985, 10, 5),
        employee_names = (alice.name, bob.name),
)

# for employee in (alice, bob, lenny):
#     employee.save()

employees = (alice, bob, lenny)

print('BEFORE SAVE')
for employee in employees:
    print(employee)

mango.save_many(*employees)


print('EVERYTHING IN THE SAME COLLECTION AS EMPLOYEE')
for employee in Employee.collection.find():
    print(employee)
    print()


print('ALL EMPLOYEES')
for employee in Employee().find_matching():
    print(employee)
    print()

print('ALL MANAGERS')
for manager in Manager.find():
    print(manager)

