from dataclasses import dataclass
from enum import Enum
from typing import Optional

from server.models.root_fs_info import RootFSInfo

# Variables
API_SUB_PATH = "/api/v1"
ECP_WORK_FILES_DIR = "ECP_WORK_FILES_DIR"
ECP_APP_FILES_DIR = "ECP_APP_FILES_DIR"
WEBUI_WORK_FILES_DIR = "WEBUI_WORK_FILES_DIR"
WEBUI_APP_FILES_DIR = "WEBUI_APP_FILES_DIR"
DATABASE_DIR = "DATABASE_DIR"


class Paths(Enum):
    """This Enumeration represents the paths used in the webui."""

    DEFAULT_ECP_WORK_DIR = "/mnt/persistent-data/evtec-ecp-data"
    DEFAULT_ECP_APP_DIR = "/opt/ECP"
    DEFAULT_WEBUI_WORK_DIR = "/mnt/persistent-data/evtec-webui-data"
    DEFAULT_WEBUI_APP_DIR = "/opt/webui"
    DEFAULT_DATABASE_DIR = "/mnt/persistent-data/evtec-webui-data/database/webui.db"
    ECP_CONFIGS_SUBDIR = "config"
    ECP_LOGS_SUBDIR = "log"
    ECP_DATA_SUBDIR = "data"
    WEBUI_WORK_SUBDIR = "work"
    PROMOTION_SCREEN_SUBDIR = "media/promotion-screen-graphics"
    CUSTOM_GRAPHICS_SUBDIR = "media/custom-graphics"
    ECP_BASE_CONFIGS_SUBDIR = "config/data"
    WEBUI_LOGS_SUBDIR = "log"
    TEMP_UPLOAD_PATH = "/tmp"
    SDCARD_CID_PATH = "/sys/block/mmcblk0/device/cid"
    STATUS_FILE = "/var/lib/opkg/status"
    OS_RELEASE = "/etc/os-release"
    UPLOAD_DIR = "/mnt/persistent-data/uploads"
    WEBUI_IPK_FILES_DIR = "/mnt/persistent-data/uploads/packages"
    WEBUI_DELTA_FM_DIR = "/mnt/persistent-data/evtec-ecp-data/certificates/delta-fm"
    WEBUI_CENTRAL_SYSTEM_CERTS_DIR = "/mnt/persistent-data/evtec-ecp-data/certificates/cs_root"
    WEBUI_RAUC_WORK_DIR = "/mnt/persistent-data/evtec-webui-data/rauc-work"


class Groups(Enum):
    """This Enum contains all the groups of the webui."""

    DOWNLOAD_CONFIG = "downloadConfig"
    UPLOAD_CONFIG = "uploadConfig"
    EDIT_CONFIG = "editConfig"
    DOWNLOAD_LOGS = "downloadLogs"
    DELETE_LOGS = "deleteLogs"
    DOWNLOAD_CUSTOM_GRAPHICS = "downloadCustomGraphics"
    UPLOAD_CUSTOM_GRAPHICS = "uploadCustomGraphics"
    EDIT_CUSTOM_GRAPHICS = "editCustomGraphics"
    DOWNLOAD_PROMOTION_SCREEN = "downloadPromotionScreen"
    UPLOAD_PROMOTION_SCREEN = "uploadPromotionScreen"
    EDIT_PROMOTION_SCREEN = "editPromotionScreen"
    TRIGGER_SYSTEM_ACTIONS = "triggerSystemActions"
    TRIGGER_USER_ACTIONS = "triggerUserActions"
    DOWNLOAD_FIRMWARE = "downloadFirmware"
    UPLOAD_FIRMWARE = "uploadFirmware"
    EDIT_FIRMWARE = "editFirmware"
    DOWNLOAD_CERTIFICATE = "downloadCertificate"
    UPLOAD_CERTIFICATE = "uploadCertificate"
    EDIT_CERTIFICATE = "editCertificate"
    TOKEN_ADMINISTRATION = "tokenAdministration"


class TokenDefinitions(Enum):
    """This Enum contains all the parameters for the tokens."""

    TOKEN_LENGTH = 200  # 200 characters
    TOKEN_LIFETIME = 7  # 7 days
    HISTORY_STATE_CURRENT = "C"
    HISTORY_STATE_HISTORY = "H"


class ErrorTypes(Enum):
    """This Enum contains all the error types."""

    ACTION_NOT_FOUND = "actionNotFoundError"
    EXCEPTION = "exceptionError"
    FILE_NOT_FOUND = "fileNotFoundError"
    FIELD_LENGTH_EXCEEDED = "fieldLengthExceededError"
    INJECTION_DETECTED = "injectionDetectedError"
    REQUEST_IS_EMPTY = "requestIsEmptyError"
    TIMESTAMP_FORMAT_ERROR = "timestampFormatError"
    TIMESTAMP_VALIDATION_ERROR = "timestampValidationError"
    TOKEN_MISSING = "tokenMissingError"
    TOKEN_INFO_MISSING = "tokenInfoMissingError"
    TOKEN_NAME_EXISTS_ERROR = "tokenNameExistsError"
    TOKEN_NAME_NOT_EXISTS_ERROR = "tokenNameNotExistsError"
    TOKEN_NOT_NECESSARY = "tokenNotNecessaryError"
    TOKEN_NOT_EXISTS = "tokenNotExistsError"
    TOKEN_NOT_PRIVILEGED = "tokenNotPrivilegedError"
    USER_NOT_FOUND = "userNotFoundError"
    WRONG_PASS = "passwordWrongError"
    WRONG_IP_FORMAT = "wrongIpFormatError"
    WRONG_URL_FORMAT = "wrongUrlFormatError"
    WRONG_IP_URL_FORMAT = "wrongIpUrlFormatError"


class Messages(Enum):
    """This Enum contains all the messages."""

    ACTION_NOT_FOUND = "Your given action was not found."
    CONNECT_VIA_WS = "Connect via ws for continue."
    INJECTION_DETECTED = "You tried to inject, bad guy you are."
    FILE_NOT_FOUND = "File not found."
    FIELD_LENGTH_EXCEEDED = "The field length was exceeded for the value '{0}'. Max length is {1}."
    PASS_NOT_MATCH = "Password does not match."
    REQUEST_IS_EMPTY = "You tried to to post without arguments."
    TIMESTAMP_FORMAT = "The given timestamp '{0}' does not correspond to a valid timestamp. Please try in this format e.g. '2024-12-27 15:00:00' or check the values for error."
    TIMESTAMP_VALIDATION = "The timestamps are not valid. Timestamp '{0}' should be bigger then '{1}'"
    TOKEN_MISSING = "The token is missing. Put in Header {X-API-TOKEN: 'yourToken'}."
    TOKEN_INFO_MISSING = "Please provide the tokenName, user, validityStart and validityEnd."
    TOKEN_NAME_EXISTS = "The given tokenName: '{0}' already exists in the database."
    TOKEN_NAME_NOT_EXISTS = "The given tokenName: '{0}' doesn't exist in the database."
    TOKEN_NOT_NECESSARY = "The post request does create the token. Please leave the field token empty"
    TOKEN_NOT_EXISTS = "The given token does not exist in the database."
    TOKEN_NOT_PRIVILEGED = "The given token does not have the privileges for this action."
    USER_NOT_FOUND = "User not found."
    UPLOAD_SUCCESSFUL = "Successful upload."
    WRONG_IP_FORMAT = "No IP address was given or bad format. e.g. '1.1.1.1'"
    WRONG_URL_FORMAT = "No URL was given or bad format."
    WRONG_IP_URL_FORMAT = "No valid IP or URL was given."


class ControllerTypes(Enum):
    """This Enum contains all the different controller types."""

    VEHICLE_CONTROLLER = "VEHICLE"
    STORAGE_CONTROLLER = "STORAGE"
    POWERSINK_CONTROLLER = "POWERSINK"
    METER_CONTROLLER = "METER"
    DCMS_CONTROLLER = "DCMS"
    DPP_CONTROLLER = "DPP"


class SystemdServices(Enum):
    """This Enum contains all the different systemd services."""

    DCMS = "DCMSControl.service"
    DISPLAY = "Display.service"
    ANDCHARGE_DISPLAY = "AndchargeDisplay.service"
    CM_DISPLAY = "CMDisplay.service"
    ECP = "ECP.service"
    GPS = "GPS.service"
    HMI_CONTROL = "HmiControl.service"
    LAN = "LAN.service"
    METER_CONTROL = "MeterControl.service"
    MOBILE_BORADBAND = "MobileBroadband.service"
    MSI_CONTROL = "MSIControl.service"
    POWERSINK_CONTROL = "PowerSinkControl.service"
    PPTP = "PPTP.service"
    STORAGE_CONTROL = "StorageControl.service"
    VEHICLE_CONTROL = "VehicleControl.service"


@dataclass
class ApplicationData:
    deviceId: Optional[str] = "UNKNOWN"
    msiiVersion: Optional[str] = "UNKNOWN"
    osReleaseVersion: Optional[str] = "UNKNOWN"
    rescueOsVersion: Optional[str] = "UNKNOWN"
    serial: Optional[str] = "UNKNOWN"
    ecpVersion: Optional[str] = "UNKNOWN"
    role: Optional[str] = "UNKNOWN"
    raucData: RootFSInfo = RootFSInfo()


@dataclass
class SystemdData:
    name: str
    description: str
    status_loaded: str
    status_active: str
    status_running: str
