# -*- coding: utf-8 -*-

from odoo import api, fields, models


class FusionCalendarEventLink(models.Model):
    _name = 'fusion.calendar.event.link'
    _description = 'Calendar Event to External Account Link'
    _order = 'x_fc_last_synced desc'

    x_fc_event_id = fields.Many2one(
        'calendar.event', string='Calendar Event',
        required=True, ondelete='cascade', index=True,
    )
    x_fc_account_id = fields.Many2one(
        'fusion.calendar.account', string='Calendar Account',
        required=True, ondelete='cascade', index=True,
    )
    x_fc_external_id = fields.Char(
        string='External Event ID', required=True, index=True,
    )
    x_fc_universal_id = fields.Char(
        string='Universal Event ID (iCalUID)', index=True,
    )
    x_fc_last_synced = fields.Datetime(string='Last Synced')
    x_fc_sync_direction = fields.Selection([
        ('pull', 'Pulled from External'),
        ('push', 'Pushed to External'),
        ('both', 'Bidirectional'),
    ], string='Sync Direction', default='pull')

    _unique_account_external = models.Constraint(
        'UNIQUE(x_fc_account_id, x_fc_external_id)',
        'An external event can only be linked once per account.',
    )
