
from odoo import fields, models, api
from odoo.osv import expression

SUPPLEMENTARY_UNITS = [
    ('c/k', 'c/k'),
    ('ce/el', 'ce/el'),
    ('ct/l', 'ct/l'),
    ('g', 'g'),
    ('gi F / S', 'gi F / S'),
    ('kg H2O2', 'kg H2O2'),
    ('kg K2O', 'kg K2O'),
    ('kg KOH', 'kg KOH'),
    ('kg met.am.', 'kg met.am.'),
    ('kg N', 'kg N'),
    ('kg NaOH', 'kg NaOH'),
    ('kg/net eda', 'kg/net eda'),
    ('kg P2O5', 'kg P2O5'),
    ('kg 90 % sdt', 'kg 90 % sdt'),
    ('kg U', 'kg U'),
    ('1 000 kWh', '1 000 kWh'),
    ('l', 'l'),
    ('l alc. 100 %', 'l alc. 100 %'),   # Litre pure (100 %) alcohol
    ('m', 'm'),                         # Metre
    ('m2', 'm2'),                       # Square metre
    ('m3', 'm3'),                       # Cubic metre
    ('1 000 m3', '1 000 m3'),           # Thousand cubic metres
    ('pa', 'pa'),                       # Number of pairs
    ('p/st', 'p/st'),                   # Number of items
    ('100 p/st', '100 p/st'),           # Hundred items
    ('1 000 p/st', '1 000 p/st'),       # Thousand items
    ('TJ', 'TJ'),                       # Terajoule (gross calorific value)
    ('t. CO2', 't. CO2'),               # Tonne of CO2 (carbon dioxide) equivalent
]


class AccountIntrastatCode(models.Model):

    _name = 'account.intrastat.code'
    _description = 'Intrastat Code'
    _translate = False
    _order = "code"
    _rec_names_search = ['code', 'name', 'description']

    name = fields.Char(string='Name')
    code = fields.Char(string='Code', required=True)
    country_id = fields.Many2one('res.country', string='Country', help='Restrict the applicability of code to a country.', domain="[('intrastat', '=', True)]")
    description = fields.Char(string='Description')
    type = fields.Selection(string='Type', required=True,
        selection=[('commodity', 'Commodity'), ('transport', 'Transport'), ('transaction', 'Transaction'), ('region', 'Region')],
        default='commodity',
        help='''Type of intrastat code used to filter codes by usage.
            * commodity: Code to be set on invoice lines for European Union statistical purposes.
            * transport: The active vehicle that moves the goods across the border.
            * transaction: A movement of goods.
            * region: A sub-part of the country.
        ''')
    supplementary_unit = fields.Selection(string='Supplementary Unit', selection=SUPPLEMENTARY_UNITS)
    expiry_date = fields.Date(
        string='Expiry Date',
        help='Date at which a code must not be used anymore.',
    )
    start_date = fields.Date(
        string='Usage start date',
        help='Date from which a code may be used.',
    )

    @api.depends('code', 'description')
    def _compute_display_name(self):
        for r in self:
            text = r.name or r.description
            r.display_name = f'{r.code} {text}' if text else r.code

    _sql_constraints = [
        ('intrastat_region_code_unique', 'UNIQUE (code, type, country_id)', 'Triplet code/type/country_id must be unique.'),
    ]
