import json
from dataclasses import dataclass
from enum import Enum


class ImportType(Enum):
    COLLAR = "collar"
    SURVEY = "survey"
    ASSAY = "assay"


@dataclass
class ImportProfile:
    """
    Class for describing import profile.
    Args:
        - id : integer (primary key)
        - name: profile's name
        - type: collar,survey or assay
        - assay_name: assay table name if type is assay, else None
        - parameters : JSON-like import parameters
    """

    id: int
    name: str
    type: ImportType
    assay_name: str
    parameters: str


class ScheduleType(Enum):
    MINUTE = "minute"
    HOURLY = "hour"
    DAILY = "day"
    MONTHLY = "month"


@dataclass
class JobFrequency:
    """
    Class describing job frequency.
    Args:
        - type : ScheduleType
        - interval : int

    """

    type: ScheduleType
    interval: int

    @staticmethod
    def possible_intervals(type: ScheduleType) -> list:
        """
        Return possible intervals for a schedule type.
        """
        if type == ScheduleType.MINUTE:
            return [i for i in range(1, 60)]
        elif type == ScheduleType.HOURLY:
            return [i for i in range(1, 24)]
        elif type == ScheduleType.DAILY:
            return [i for i in range(1, 32)]
        else:
            return [i for i in range(1, 13)]

    def to_json(self) -> str:
        d = {
            "type": self.type.value,
            "interval": self.interval,
        }
        return json.dumps(d)

    @classmethod
    def from_json(cls, json_str: str):
        d = json.loads(json_str)
        return cls(ScheduleType(d["type"]), d["interval"])


@dataclass
class AutomatedImport:
    """
    Class describing automated import.
    """

    id: int
    name: str
    profile: int
    source_folder: str
    person: str
    dataset: str
    frequency: JobFrequency
    log_folder: str
    creation_date: str
    last_run_date: str
    run_count: int
    failed_run_count: int
