import stdnum.exceptions
from sql import Column, Literal
import datetime
from sql.aggregate import Count, Max, Min, Sum, Avg, BoolOr
from sql.conditionals import Case
from stdnum import get_cc_module
from sql.functions import CharLength, CurrentTimestamp, DateTrunc, Extract
from trytond.i18n import gettext
from trytond.model import (
    DeactivableMixin, Index, ModelSQL, ModelView, MultiValueMixin, Unique,
    ValueMixin, convert_from, fields, sequence_ordered)
from trytond.model.exceptions import AccessError
from trytond.pool import Pool
from trytond.pyson import Bool, Eval
from trytond.tools import is_full_text, lstrip_wildcard
from trytond.transaction import Transaction, inactive_records
from trytond.wizard import Button, StateTransition, StateView, Wizard

VARTYPE = [
    ('historical', 'Historical'),
    ('parametric', 'Parametric'),
    ('montecarlo', 'Monté Carlo'),
    ('garch', 'GARCH'),
]

VARMODEL = [
    ('norm', 'Normal'),
    ('lognormal', 'Log Normal'),
    ('student', 'T student'),
    ('uniform', 'Uniform'),
    ('expon', 'Exponential'),
    ('gamma', 'Gamma'),
    ('chi2', 'Chi2'),
]

class Var(ModelSQL, ModelView):
    "Value at risk"
    __name__ = 'risk.var'

    name = fields.Char("Name")
    horizon = field.Integer("Time horizon")
    confidence = fields.Numeric("Confidence")
    type = fields.Selection(VARTYPE,"Type")
    from_date = fields.Date("From")
    to_date = fields.Date("To")
    nb_days = fields.Integer("Nb days")
    lines = fields.One2Many('risk.var.line','var',"Lines")
    values = fields.One2Many('risk.var.values','var',"Values")
    product = fields.Many2One('product.product',"Product")
    party = fields.Many2One('party.party',"Trader")

class VarLine(ModelSQL, ModelView):
    "VaR line"
    __name__ = 'risk.var.line'

    var = fields.Many2One('risk.var',"Var")
    curve = fields.Many2One('price.price',"Curve")
    model = fields.Selection(VARMODEL,"Model")
    weight = fields.Numeric("Weight")
    dfn = fields.Numeric("Dfn")
    dfn = fields.Numeric("Dfd")

class VarValue(ModelSQL, ModelView):
    "VaR value"
    __name__ = 'risk.var.values'

    var = fields.Many2One('risk.var',"Var")
    product = fields.Many2One('product.product',"Product")
    party = fields.Many2One('party.party',"Trader")
    r_var = fields.Numeric("VaR")
    r_es = fields.Numeric("ES")
    r_cdar = fields.Numeric("CDaR")

