from sqlalchemy import literal
from changes.config import db
from changes.models.option import ItemOption
from changes.models.plan import Plan, PlanStatus
from changes.models.project import Project  # NOQA


def contains_active_autogenerated_plan(project):
    """Returns true if there are any active autogenerated build plans for
    this project.
    """
    # type: (Project)->bool
    contains = db.session.query(literal(True)).filter(
        ItemOption.query.join(
            Plan, Plan.id == ItemOption.item_id,
        ).filter(
            ItemOption.name == 'bazel.autogenerate',
            ItemOption.value == '1',
            Plan.project_id == project.id,
            Plan.status == PlanStatus.active,
        ).exists()
    ).scalar()
    return bool(contains)
