# The AIConsole Project
#
# Copyright 2023 10Clouds
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

import rtoml
from aiconsole.api.websockets.outgoing_messages import NotificationWSMessage
from aiconsole.core.assets.agents.agent import Agent
from aiconsole.core.assets.asset import Asset, AssetLocation, AssetType
from aiconsole.core.gpt.consts import GPTMode
from aiconsole.core.assets.materials.material import Material, MaterialContentType
from aiconsole.core.project.paths import (get_core_assets_directory,
                                          get_project_assets_directory)


async def load_asset_from_fs(asset_type: AssetType, asset_id: str) -> Asset:
    """
    Load a specific asset.
    """

    project_dir_path = get_project_assets_directory(asset_type)
    core_resource_path = get_core_assets_directory(asset_type)

    if (project_dir_path / f"{asset_id}.toml").exists():
        # if asset exists in core
        if (core_resource_path / f"{asset_id}.toml").exists():
            await NotificationWSMessage(
                title=f"Asset {asset_id} exists in core and project directory",
                message="Project directory version will be used.",
            ).send_to_all()
        location = AssetLocation.PROJECT_DIR
        path = project_dir_path / f"{asset_id}.toml"
    elif (core_resource_path / f"{asset_id}.toml").exists():
        location = AssetLocation.AICONSOLE_CORE
        path = core_resource_path / f"{asset_id}.toml"
    else:
        raise KeyError(f"Asset {asset_id} not found")

    with open(path, "r") as file:
        tomldoc = rtoml.loads(file.read())

    asset_id = os.path.splitext(os.path.basename(path))[0]

    if asset_type == AssetType.MATERIAL:
        material = Material(
            id=asset_id,
            version=str(tomldoc.get("version", "0.0.1")).strip(),
            name=str(tomldoc.get("name", asset_id)).strip(),
            defined_in=location,
            usage=str(tomldoc["usage"]).strip(),
            content_type=MaterialContentType(
                str(tomldoc["content_type"]).strip()))

        if "content_static_text" in tomldoc:
            material.content_static_text = str(
                tomldoc["content_static_text"]).strip()

        if "content_dynamic_text" in tomldoc:
            material.content_dynamic_text = str(
                tomldoc["content_dynamic_text"]).strip()

        if "content_api" in tomldoc:
            material.content_api = str(tomldoc["content_api"]).strip()

        return material

    if asset_type == AssetType.AGENT:
        if asset_id == "user":
            raise KeyError("Agent 'user' is reserved.")
        
        params = {}

        params["id"] = asset_id

        if "version" in tomldoc:
            params["version"] = str(tomldoc.get("version")).strip()
        
        params["name"] = str(tomldoc.get("name", asset_id)).strip()
        params["defined_in"] = location
        params["usage"] = str(tomldoc["usage"]).strip()
        params["system"] = str(tomldoc["system"]).strip()

        if "gpt_mode" in tomldoc:
            params["gpt_mode"] = GPTMode(str(tomldoc["gpt_mode"]).strip())

        if "execution_mode" in tomldoc:
            params["execution_mode"] = str(tomldoc["execution_mode"]).strip()

        agent = Agent(**params)

        return agent

    raise Exception(f"Asset type {asset_type} not supported.")
