# This file is part of Tradon. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.

from trytond.model import ModelSQL, ModelView, fields, sequence_ordered

__all__ = ['TemplateSegment', 'ExecutionTemplate', 'ExecutionTemplateLine']


class TemplateSegment(ModelSQL, ModelView):
    'Template Segment'
    __name__ = 'trade_finance.template_segment'
    _rec_name = 'name'

    code = fields.Char('Code', required=True)
    name = fields.Char('Name', required=True)
    from_place = fields.Many2One(
        'stock.location', 'From Place',
        help='Origin location / port of loading')
    to_place = fields.Many2One(
        'stock.location', 'To Place',
        help='Destination location / port of discharge')
    default_duration_days = fields.Integer(
        'Default Duration (days)',
        help='Default number of days for this segment')
    active = fields.Boolean('Active')

    @staticmethod
    def default_active():
        return True


class ExecutionTemplate(ModelSQL, ModelView):
    'Execution Template'
    __name__ = 'trade_finance.execution_template'
    _rec_name = 'name'

    code = fields.Char('Code', required=True)
    name = fields.Char('Name', required=True)
    description = fields.Text('Description')
    lines = fields.One2Many(
        'trade_finance.execution_template_line', 'template', 'Segments')
    active = fields.Boolean('Active')

    @staticmethod
    def default_active():
        return True


class ExecutionTemplateLine(sequence_ordered(), ModelSQL, ModelView):
    'Execution Template Line'
    __name__ = 'trade_finance.execution_template_line'

    template = fields.Many2One(
        'trade_finance.execution_template', 'Template',
        required=True, ondelete='CASCADE')
    segment = fields.Many2One(
        'trade_finance.template_segment', 'Segment',
        required=True, ondelete='RESTRICT')
    duration_days = fields.Integer(
        'Duration (days)',
        help='Overrides the segment default duration for this template')
