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

from .common import Base, session_scope

from sqlalchemy import and_


class Contract(Base):
    __tablename__ = 'contracts'
    __autoload__ = True

    STANDARD_ATTRS = [
        'signing_team_id', 'signing_date', 'length', 'value', 'type',
        'expiry_status', 'source', 'start_season', 'end_season',
        'cap_hit_percentage', 'bought_out'
        ]

    def __init__(self, player_id, contract_data_dict):

        self.player_id = player_id
        for attr in self.STANDARD_ATTRS:
            if attr in contract_data_dict:
                setattr(self, attr, contract_data_dict[attr])

    @classmethod
    def find(self, player_id, start_season, end_season):
        with session_scope() as session:
            try:
                contract = session.query(Contract).filter(
                    and_(
                        Contract.player_id == player_id,
                        Contract.start_season == start_season,
                        Contract.end_season == end_season
                    )
                ).one()
            except:
                contract = None
            return contract

    def update(self, other):
        for attr in self.STANDARD_ATTRS:
            if hasattr(other, attr):
                setattr(self, attr, getattr(other, attr))

    def __eq__(self, other):
        return ((
            self.player_id, self.signing_team_id, self.signing_date,
            self.length, self.value, self.type, self.expiry_status,
            self.source, self.start_season, self.end_season,
            "%.2f" % round(self.cap_hit_percentage, 2), self.bought_out
            ) == (
            other.player_id, other.signing_team_id, other.signing_date,
            other.length, other.value, other.type, other.expiry_status,
            other.source, other.start_season, other.end_season,
            "%.2f" % round(other.cap_hit_percentage, 2), other.bought_out))

    def __ne__(self, other):
        return not self == other

@@ -390,16 +390,25 @@
 ght_out'
+, 'notes'
 %0A       
@@ -1573,32 +1573,44 @@
 self.end_season,
+ self.notes,
 %0A            %22%25.
@@ -1884,32 +1884,45 @@
 ther.end_season,
+ other.notes,
 %0A            %22%25.

Add attribute to contract item