from controllers.base import Controller
from resources import types
from controllers.notices import Notices
from controllers.marks import action


class Rules(Controller):
    resource = types.Rule

    @action
    def inject(self, n: int):
        return self.update(n, injected=True)

    @action
    def uninject(self, n: int):
        return self.update(n, injected=False)

    @action
    def pin(self, n: int):
        rule = self.peek(n)
        notices = Notices(self.record, actor=self.actor)
        standing = [notice for notice in notices.linked_to(rule.ref) if not notice.completed]
        return standing[0] if standing else notices.create(rule.title, about=rule.ref, link=f"#/{self.record.env}/rule/{n}", label="Open rule")
