from django.db import models
import os, errno

from api.utils import *

class PlanManager(models.Manager):
    def statistic(self, user):
        results = user.own_plans.all()
        return {
            'amount': len(results),
        }

class Plan(models.Model):
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)
    parent = models.ForeignKey('self', on_delete=models.CASCADE, related_name='own_child_plans', blank=True, null=True)
    mission = models.ForeignKey(to="api.Mission", on_delete=models.CASCADE, related_name="own_plans")
    algorithm = models.ForeignKey(to="api.Algorithm", on_delete=models.DO_NOTHING, related_name="own_plans", blank=True, null=True)

    user = models.ForeignKey(to="api.User", on_delete=models.CASCADE, related_name='own_plans')

    objects = PlanManager()

    class Meta:
        app_label = 'api'