from trytond.model import ModelSQL, ModelView, fields
from trytond.pool import Pool
from datetime import datetime

class LCDocumentReceived(ModelSQL, ModelView):
    'LC Document Received'
    __name__ = 'lc.document.received'

    lc = fields.Many2One('lc.letter.outgoing', 'LC', required=True)
    document_type = fields.Many2One('lc.document.type', 'Document Type', required=True)
    received = fields.Boolean('Received')
    received_date = fields.Date('Received Date')
    document_ref = fields.Char('Document Reference')
    attachment = fields.Many2One('ir.attachment', 'Attachment')
    discrepancy = fields.Boolean('Discrepancy')
    discrepancy_details = fields.Text('Discrepancy Details')
    status = fields.Selection([
        ('pending', 'Pending'),
        ('received', 'Received'),
        ('checked', 'Checked'),
        ('discrepant', 'Discrepant'),
        ('accepted', 'Accepted')
    ], 'Status')
    notes = fields.Text('Notes')

    @staticmethod
    def default_status():
        return 'pending'

    @staticmethod
    def default_received():
        return False

    @classmethod
    def create(cls, vlist):
        vlist = [v.copy() for v in vlist]
        for vals in vlist:
            if vals.get('received') and not vals.get('received_date'):
                vals['received_date'] = datetime.now().date()
            if vals.get('received') and vals.get('status') == 'pending':
                vals['status'] = 'received'
        return super().create(vlist)

class LCDocumentPrepared(ModelSQL, ModelView):
    'LC Document Prepared'
    __name__ = 'lc.document.prepared'

    lc = fields.Many2One('lc.letter.incoming', 'LC', required=True)
    document_type = fields.Many2One('lc.document.type', 'Document Type', required=True)
    required = fields.Boolean('Required')
    responsible = fields.Many2One('res.user', 'Responsible')
    deadline = fields.Date('Deadline')
    prepared = fields.Boolean('Prepared')
    prepared_date = fields.Date('Prepared Date')
    attachment = fields.Many2One('ir.attachment', 'Attachment')
    status = fields.Selection([
        ('pending', 'Pending'),
        ('in_progress', 'In Progress'),
        ('prepared', 'Prepared'),
        ('presented', 'Presented'),
        ('accepted', 'Accepted')
    ], 'Status')
    notes = fields.Text('Notes')

    @staticmethod
    def default_status():
        return 'pending'

    @staticmethod
    def default_required():
        return True

    @staticmethod
    def default_prepared():
        return False

class LCDiscrepancy(ModelSQL, ModelView):
    'LC Discrepancy'
    __name__ = 'lc.discrepancy'

    lc = fields.Many2One('lc.letter.incoming', 'LC', required=True)
    document_type = fields.Many2One('lc.document.type', 'Document Type')
    discrepancy_type = fields.Selection([
        ('document', 'Document Discrepancy'),
        ('date', 'Date Discrepancy'),
        ('amount', 'Amount Discrepancy'),
        ('condition', 'Condition Discrepancy'),
        ('other', 'Other')
    ], 'Discrepancy Type', required=True)
    description = fields.Text('Description', required=True)
    severity = fields.Selection([
        ('minor', 'Minor'),
        ('major', 'Major'),
        ('critical', 'Critical')
    ], 'Severity')
    resolved = fields.Boolean('Resolved')
    resolution = fields.Text('Resolution')
    resolved_date = fields.Date('Resolved Date')
    resolved_by = fields.Many2One('res.user', 'Resolved By')

    @staticmethod
    def default_severity():
        return 'minor'

    @staticmethod
    def default_resolved():
        return False