from handshake.services.DBService.models.result_base import SuiteBase, RunBase
from tortoise.models import Model
from tortoise.fields import (
    ForeignKeyField,
    ForeignKeyRelation,
    TextField,
    IntField,
    DatetimeField,
    CharField,
    BooleanField,
    CharEnumField,
    JSONField,
    IntEnumField,
)
from handshake.services.DBService.models.enums import LogType, LogGeneratedBy


class AssertBase(Model):
    entity: ForeignKeyRelation[SuiteBase] = ForeignKeyField(
        "models.SuiteBase", related_name="assertion", to_field="suiteID"
    )
    passed = BooleanField(
        default=False, description="Whether the assertion passed or not"
    )
    wait = IntField(
        description="Number of milli-seconds configured to wait for this test",
        default=-1,
        null=True,
    )
    interval = IntField(
        description="interval (in milli-seconds) to test this assertion until it passes",
        default=-1,
        null=True,
    )
    title = TextField(description="Name of the Assertion")
    message = TextField(description="Message attached to the assertion")


class CommonLogBase(Model):
    type = CharEnumField(LogType, description="Log type", null=False)
    title = TextField(description="title for the log", null=False)
    message = TextField(description="formatted log message", default="", null=True)
    feed = JSONField(
        description="If you want to share any feed here", null=True, default={}
    )
    generatedBy = CharField(
        max_length=80,
        description="which job generated this, Null if generated by the user",
        null=False,
    )
    generatedByGroup = IntEnumField(
        LogGeneratedBy,
        default=LogGeneratedBy.USER,
        description="generated by api/scheduler jobs/user",
    )
    tags = JSONField(
        description="comma separated list of tags used by the framework to filter the suites or spec files",
        default=[],
        null=True,
    )
    dropped = DatetimeField(auto_now=True, description="timestamp", null=False)

    class Meta:
        abstract = True


class EntityLogBase(CommonLogBase):
    entity: ForeignKeyRelation[SuiteBase] = ForeignKeyField(
        "models.SuiteBase", related_name="entityLog", to_field="suiteID"
    )


class TestLogBase(CommonLogBase):
    test: ForeignKeyRelation[RunBase] = ForeignKeyField(
        "models.RunBase", related_name="log", to_field="testID", pk=True
    )
