from uuid import uuid4 as uuid

from peewee import BooleanField, TextField
from playhouse.sqlite_ext import JSONField

from .base_model import BaseModel
from .steps import Steps


class Recipes(BaseModel):
    name = TextField(primary_key=True, unique=True, null=False, default=lambda: uuid().hex)
    client = TextField(null=True)
    part_number = TextField(null=False)
    spec = JSONField(null=False)  # keys inside spec must not overlap withthe model
    description = TextField(null=True)
    archived = BooleanField(null=False, default=False)

    def get_steps(self):
        s_pks = self.spec.get("steps", [])
        s = {s.name: s for s in Steps.select().where(Steps.name << s_pks)}
        return [s[s_id] for s_id in s_pks]

    @classmethod
    def delete(cls, *args, **kwargs):
        # OVERRIDE DELETION
        # so that deleting a user will only archive it
        return cls.update(archived=True)

    class Meta:
        table_name = "recipes"
